Skip to content

Commit

Permalink
Merge pull request #36 from Yoast/feature/add-error-typeerror-polyfills
Browse files Browse the repository at this point in the history
Add polyfills for the PHP 7.0 `TypeError` and `Error` classes
  • Loading branch information
jrfnl committed Jun 16, 2021
2 parents 4a6caea + 86abfb9 commit 985d7a8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,15 @@ jobs:

- name: "Lint PHP files against parse errors - PHP < 7.0"
if: ${{ matrix.phpunit == 'auto' && matrix.php < 7.0 }}
run: composer lint-lt71
run: composer lint-lt70

- name: "Lint PHP files against parse errors - PHP >= 7.0"
if: ${{ matrix.phpunit == 'auto' && matrix.php >= 7.0 }}
run: composer lint
- name: "Lint PHP files against parse errors - PHP 7.x"
if: ${{ matrix.phpunit == 'auto' && startsWith( matrix.php, '7' ) }}
run: composer lint7

- name: "Lint PHP files against parse errors - PHP >= 8.0"
if: ${{ matrix.phpunit == 'auto' && matrix.php >= 8.0 }}
run: composer lint-gte80

- name: Run the unit tests
run: composer test
11 changes: 11 additions & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<exclude name="WordPress.Security"/>
<exclude name="WordPress.WP"/>
<exclude name="Yoast.Yoast.AlternativeFunctions"/>

<!-- These PHP 7.0+ classes are polyfilled for this repo. -->
<exclude name="PHPCompatibility.Classes.NewClasses.errorFound"/>
<exclude name="PHPCompatibility.Classes.NewClasses.typeerrorFound"/>
</rule>

<!-- Enforce PSR1 compatible namespaces. -->
Expand Down Expand Up @@ -81,6 +85,13 @@
#############################################################################
-->

<!-- The polyfills for the PHP native exceptions can not have a namespace
and making those the only files with a file comment would create more,
not less inconsistency. -->
<rule ref="Yoast.Commenting.FileComment">
<exclude-pattern>/src/Exceptions/*Error\.php$</exclude-pattern>
</rule>

<!-- Deliberately empty Catch statements. -->
<rule ref="Generic.CodeAnalysis.EmptyStatement.DetectedCatch">
<exclude-pattern>/src/Helpers/ResourceHelper\.php$</exclude-pattern>
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@
}
},
"scripts": {
"lint": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git"
"lint7": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git --exclude src/Exceptions/Error.php --exclude src/Exceptions/TypeError.php"
],
"lint-lt71": [
"lint-lt70": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git --exclude src/TestCases/TestCasePHPUnitGte8.php --exclude src/TestListeners/TestListenerDefaultImplementationPHPUnitGte7.php"
],
"lint-gte80": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git"
],
"check-cs": [
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcs --runtime-set testVersion 5.4-"
],
Expand Down
21 changes: 21 additions & 0 deletions phpunitpolyfills-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ class Autoload {
* @return bool
*/
public static function load( $className ) {
/*
* Polyfill two PHP 7.0 classes.
* The autoloader will only be called for these if these classes don't already
* exist in PHP natively.
*/
if ( $className === 'Error' || $className === 'TypeError' ) {
$file = \realpath(
__DIR__ . \DIRECTORY_SEPARATOR
. 'src' . \DIRECTORY_SEPARATOR
. 'Exceptions' . \DIRECTORY_SEPARATOR
. $className . '.php'
);

if ( \file_exists( $file ) === true ) {
require_once $file;
return true;
}

return false;
}

// Only load classes belonging to this library.
if ( \stripos( $className, 'Yoast\PHPUnitPolyfills' ) !== 0 ) {
return false;
Expand Down
8 changes: 8 additions & 0 deletions src/Exceptions/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
* Polyfill the PHP 7.0+ native Error class.
*
* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/
class Error extends Exception {}
8 changes: 8 additions & 0 deletions src/Exceptions/TypeError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
* Polyfill the PHP 7.0+ native TypeError class.
*
* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/
class TypeError extends Error {}

0 comments on commit 985d7a8

Please sign in to comment.