Skip to content

Commit a395e76

Browse files
authored
Merge pull request #939 from ergebnis/feature/invoke-parent-method
Enhancement: Implement `InvokeParentHookMethodRule`
2 parents 803668e + e91707e commit a395e76

File tree

43 files changed

+3499
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3499
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88

99
For a full diff see [`2.9.0...main`][2.9.0...main].
1010

11+
### Added
12+
13+
- Added `Methods\InvokeParentHookMethodRule`, which reports an error when a hook method that overrides a hook method in a parent class does not invoke the overridden hook method in the expected order ([#939]), by [@localheinz]
14+
1115
## [`2.9.0`][2.9.0]
1216

1317
For a full diff see [`2.8.0...2.9.0`][2.8.0...2.9.0].
@@ -640,6 +644,7 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
640644
[#913]: https://github.com/ergebnis/phpstan-rules/pull/913
641645
[#914]: https://github.com/ergebnis/phpstan-rules/pull/914
642646
[#938]: https://github.com/ergebnis/phpstan-rules/pull/938
647+
[#939]: https://github.com/ergebnis/phpstan-rules/pull/939
643648

644649
[@cosmastech]: https://github.com/cosmastech
645650
[@enumag]: https://github.com/enumag

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ This package provides the following rules for use with [`phpstan/phpstan`](https
6464
- [`Ergebnis\PHPStan\Rules\Functions\NoParameterWithNullDefaultValueRule`](https://github.com/ergebnis/phpstan-rules#functionsnoparameterwithnulldefaultvaluerule)
6565
- [`Ergebnis\PHPStan\Rules\Functions\NoReturnByReferenceRule`](https://github.com/ergebnis/phpstan-rules#functionsnoreturnbyreferencerule)
6666
- [`Ergebnis\PHPStan\Rules\Methods\FinalInAbstractClassRule`](https://github.com/ergebnis/phpstan-rules#methodsfinalinabstractclassrule)
67+
- [`Ergebnis\PHPStan\Rules\Methods\InvokeParentHookMethodRule`](https://github.com/ergebnis/phpstan-rules#methodsinvokeparenthookmethodrule)
6768
- [`Ergebnis\PHPStan\Rules\Methods\NoConstructorParameterWithDefaultValueRule`](https://github.com/ergebnis/phpstan-rules#methodsnoconstructorparameterwithdefaultvaluerule)
6869
- [`Ergebnis\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRule`](https://github.com/ergebnis/phpstan-rules#methodsnonullablereturntypedeclarationrule)
6970
- [`Ergebnis\PHPStan\Rules\Methods\NoParameterPassedByReferenceRule`](https://github.com/ergebnis/phpstan-rules#methodsnoparameterpassedbyreferencerule)
@@ -446,6 +447,63 @@ parameters:
446447
enabled: false
447448
```
448449

450+
#### `Methods\InvokeParentHookMethodRule`
451+
452+
This rule reports an error when a hook method that overrides a hook method in a parent class does not invoke the overridden hook method in the expected order.
453+
454+
##### Defaults
455+
456+
By default, this rule requires the following hook methods to be invoked before doing something in the overriding method:
457+
458+
- [`Codeception\PHPUnit\TestCase::_setUp()`](https://github.com/Codeception/phpunit-wrapper/blob/9.0.0/src/TestCase.php#L11-L13)
459+
- [`Codeception\PHPUnit\TestCase::_setUpBeforeClass()`](https://github.com/Codeception/phpunit-wrapper/blob/9.0.0/src/TestCase.php#L25-L27)
460+
- [`Codeception\Test\Unit::_before()`](https://github.com/Codeception/Codeception/blob/4.2.2/src/Codeception/Test/Unit.php#L63-L65)
461+
- [`Codeception\Test\Unit::_setUp()`](https://github.com/Codeception/Codeception/blob/4.2.2/src/Codeception/Test/Unit.php#L34-L58)
462+
- [`PHPUnit\Framework\TestCase::assertPreConditions()`](https://github.com/sebastianbergmann/phpunit/blob/6.0.0/src/Framework/TestCase.php#L2073-L2075)
463+
- [`PHPUnit\Framework\TestCase::setUp()`](https://github.com/sebastianbergmann/phpunit/blob/6.0.0/src/Framework/TestCase.php#L2063-L2065)
464+
- [`PHPUnit\Framework\TestCase::setUpBeforeClass()`](https://github.com/sebastianbergmann/phpunit/blob/6.0.0/src/Framework/TestCase.php#L2055-L2057)
465+
466+
By default, this rule requires the following hook methods to be invoked after doing something in the overriding method:
467+
468+
- [`Codeception\PHPUnit\TestCase::_tearDown()`](https://github.com/Codeception/phpunit-wrapper/blob/9.0.0/src/TestCase.php#L18-L20)
469+
- [`Codeception\PHPUnit\TestCase::_tearDownAfterClass()`](https://github.com/Codeception/phpunit-wrapper/blob/9.0.0/src/TestCase.php#L32-L34)
470+
- [`Codeception\Test\Unit::_after()`](https://github.com/Codeception/Codeception/blob/4.2.2/src/Codeception/Test/Unit.php#L75-L77)
471+
- [`Codeception\Test\Unit::_tearDown()`](https://github.com/Codeception/Codeception/blob/4.2.2/src/Codeception/Test/Unit.php#L67-L70)
472+
- [`PHPUnit\Framework\TestCase::assertPostConditions()`](https://github.com/sebastianbergmann/phpunit/blob/6.0.0/src/Framework/TestCase.php#L2083-L2085)
473+
- [`PHPUnit\Framework\TestCase::tearDown()`](https://github.com/sebastianbergmann/phpunit/blob/6.0.0/src/Framework/TestCase.php#L2091-L2093)
474+
- [`PHPUnit\Framework\TestCase::tearDownAfterClass()`](https://github.com/sebastianbergmann/phpunit/blob/6.0.0/src/Framework/TestCase.php#L2098-L2100)
475+
476+
##### Disabling the rule
477+
478+
You can set the `enabled` parameter to `false` to disable this rule.
479+
480+
```neon
481+
parameters:
482+
ergebnis:
483+
invokeParentHookMethod:
484+
enabled: false
485+
```
486+
487+
##### Configuring methods to invoke the parent method in the right order:
488+
489+
You can set the `hookMethods` parameter to a list of hook methods:
490+
491+
```neon
492+
parameters:
493+
ergebnis:
494+
invokeParentHookMethod:
495+
hookMethods:
496+
- className: "Example\Test\Functional\AbstractCest"
497+
methodName: "_before"
498+
hasContent: "yes"
499+
invocation: "first"
500+
```
501+
502+
- `className`: name of the class that declares the hook method
503+
- `methodName`: name of the hook method
504+
- `hasContent`: one of `"yes"`, `"no"`, `"maybe"`
505+
- `invocation`: one of `"any"` (needs to be invoked), `"first"` (needs to be invoked before all other statements in the overriding hook method, `"last"` (needs to be invoked after all other statements in the overriding hook method)
506+
449507
#### `Methods\NoConstructorParameterWithDefaultValueRule`
450508

451509
This rule reports an error when a constructor declared in

composer-require-checker.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"symbol-whitelist": [
33
"array",
44
"bool",
5+
"Codeception\\PHPUnit\\TestCase",
6+
"Codeception\\Test\\Unit",
57
"Doctrine\\ORM\\Mapping\\Embeddable",
68
"Doctrine\\ORM\\Mapping\\Entity",
79
"false",
@@ -30,6 +32,7 @@
3032
"PhpParser\\Node\\Stmt\\Class_",
3133
"PhpParser\\Node\\Stmt\\ClassMethod",
3234
"PhpParser\\Node\\Stmt\\Declare_",
35+
"PhpParser\\Node\\Stmt\\Expression",
3336
"PhpParser\\Node\\Stmt\\Function_",
3437
"PhpParser\\Node\\Stmt\\InlineHTML",
3538
"PhpParser\\Node\\Stmt\\Switch_",

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
"phpstan/phpstan": "^2.1.8"
2727
},
2828
"require-dev": {
29+
"codeception/codeception": "^4.0.0 || ^5.0.0",
2930
"doctrine/orm": "^2.20.0 || ^3.3.0",
3031
"ergebnis/composer-normalize": "^2.47.0",
3132
"ergebnis/license": "^2.6.0",
3233
"ergebnis/php-cs-fixer-config": "^6.46.0",
3334
"ergebnis/phpunit-slow-test-detector": "^2.19.1",
35+
"fakerphp/faker": "^1.24.1",
3436
"nette/di": "^3.1.10",
3537
"phpstan/extension-installer": "^1.4.3",
3638
"phpstan/phpstan-deprecation-rules": "^2.0.3",

0 commit comments

Comments
 (0)