Skip to content

Commit fbbd15c

Browse files
authored
Merge pull request #73 from localheinz/feature/default
Enhancement: Adjust Classes\NoExtendsRule to allow to extend from a set of classes by default
2 parents 993f7e8 + 8241be1 commit fbbd15c

File tree

8 files changed

+59
-7
lines changed

8 files changed

+59
-7
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## Unreleased
88

9-
For a full diff see [`0.7.0...master`](https://github.com/localheinz/phpstan-rules/compare/0.7.0...master).
9+
For a full diff see [`0.7.1...master`](https://github.com/localheinz/phpstan-rules/compare/0.7.1...master).
10+
11+
## [`0.7.1`](https://github.com/localheinz/phpstan-rules/releases/tag/0.7.1)
12+
13+
For a full diff see [`0.7.0...0.7.1`](https://github.com/localheinz/phpstan-rules/compare/0.7.0...0.7.1).
14+
15+
### Changed
16+
17+
* Configured `Classes\NoExtendsRule` to allow a set of default classes to be extended ([#73](https://github.com/localheinz/phpstan-rules/pull/73)), by [@localheinz](https://github.com/localheinz)
1018

1119
## [`0.7.0`](https://github.com/localheinz/phpstan-rules/releases/tag/0.7.0)
1220

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,21 @@ parameters:
7777

7878
This rule reports an error when a class extends another class.
7979

80+
##### Defaults
81+
82+
By default, this rule allows the following classes to be extended:
83+
84+
* [`PHPUnit\Framework\TestCase`](https://github.com/sebastianbergmann/phpunit/blob/7.5.2/src/Framework/TestCase.php)
85+
8086
##### Allowing classes to be extended
8187

82-
If you want to allow some classes to be extended, you can set the `classesAllowedToBeExtended` parameter to a list of class names:
88+
If you want to allow additional classes to be extended, you can set the `classesAllowedToBeExtended` parameter to a list of class names:
8389

8490
```neon
8591
parameters:
8692
classesAllowedToBeExtended:
8793
- Localheinz\PHPStan\Rules\Test\Integration\AbstractTestCase
8894
- PHPStan\Testing\RuleTestCase
89-
- PHPUnit\Framework\TestCase
9095
```
9196

9297
### Closures

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ parameters:
88
classesAllowedToBeExtended:
99
- Localheinz\PHPStan\Rules\Test\Integration\AbstractTestCase
1010
- PHPStan\Testing\RuleTestCase
11-
- PHPUnit\Framework\TestCase
1211
excludes_analyse:
1312
- %currentWorkingDirectory%/test/Fixture/
1413
paths:

src/Classes/NoExtendsRule.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
final class NoExtendsRule implements Rule
2121
{
22+
/**
23+
* @var string[]
24+
*/
25+
private static $defaultClassesAllowedToBeExtended = [
26+
'PHPUnit\\Framework\\TestCase',
27+
];
28+
2229
/**
2330
* @var string[]
2431
*/
@@ -29,9 +36,12 @@ final class NoExtendsRule implements Rule
2936
*/
3037
public function __construct(array $classesAllowedToBeExtended)
3138
{
32-
$this->classesAllowedToBeExtended = \array_map(static function (string $classAllowedToBeExtended): string {
33-
return $classAllowedToBeExtended;
34-
}, $classesAllowedToBeExtended);
39+
$this->classesAllowedToBeExtended = \array_unique(\array_merge(
40+
self::$defaultClassesAllowedToBeExtended,
41+
\array_map(static function (string $classAllowedToBeExtended): string {
42+
return $classAllowedToBeExtended;
43+
}, $classesAllowedToBeExtended)
44+
));
3545
}
3646

3747
public function getNodeType(): string
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRule\Success;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class ClassExtendingPhpUnitFrameworkTestCase extends TestCase
13+
{
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Success;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class ClassExtendingPhpUnitFrameworkTestCase extends TestCase
13+
{
14+
}

test/Integration/Classes/NoExtendsRuleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function providerAnalysisSucceeds(): \Generator
2727
{
2828
$paths = [
2929
'class' => __DIR__ . '/../../Fixture/Classes/NoExtendsRule/Success/ExampleClass.php',
30+
'class-extending-php-unit-framework-test-case' => __DIR__ . '/../../Fixture/Classes/NoExtendsRule/Success/ClassExtendingPhpUnitFrameworkTestCase.php',
3031
'interface' => __DIR__ . '/../../Fixture/Classes/NoExtendsRule/Success/ExampleInterface.php',
3132
'interface-extending-other-interface' => __DIR__ . '/../../Fixture/Classes/NoExtendsRule/Success/InterfaceExtendingOtherInterface.php',
3233
'script-with-anonymous-class' => __DIR__ . '/../../Fixture/Classes/NoExtendsRule/Success/anonymous-class.php',

test/Integration/Classes/NoExtendsRuleWithClassesAllowedToBeExtendedTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function providerAnalysisSucceeds(): \Generator
2828
$paths = [
2929
'class' => __DIR__ . '/../../Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/ExampleClass.php',
3030
'class-extending-class-allowed-to-be-extended' => __DIR__ . '/../../Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/ClassExtendingClassAllowedToBeExtended.php',
31+
'class-extending-php-unit-framework-test-case' => __DIR__ . '/../../Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/ClassExtendingPhpUnitFrameworkTestCase.php',
3132
'interface' => __DIR__ . '/../../Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/ExampleInterface.php',
3233
'interface-extending-other-interface' => __DIR__ . '/../../Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/InterfaceExtendingOtherInterface.php',
3334
'script-with-anonymous-class' => __DIR__ . '/../../Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/anonymous-class.php',

0 commit comments

Comments
 (0)