-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from localheinz/feature/no-extends-rule
Enhancement: Implement Classes\NoExtendsRule
- Loading branch information
Showing
28 changed files
with
461 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/phpstan-rules | ||
*/ | ||
|
||
namespace Localheinz\PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
|
||
final class NoExtendsRule implements Rule | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private $classesAllowedToBeExtended; | ||
|
||
/** | ||
* @param string[] $classesAllowedToBeExtended | ||
*/ | ||
public function __construct(array $classesAllowedToBeExtended) | ||
{ | ||
$this->classesAllowedToBeExtended = \array_map(static function (string $classAllowedToBeExtended): string { | ||
return $classAllowedToBeExtended; | ||
}, $classesAllowedToBeExtended); | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Class_::class; | ||
} | ||
|
||
/** | ||
* @param Node\Stmt\Class_ $node | ||
* @param Scope $scope | ||
* | ||
* @return array | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->extends instanceof Node\Name) { | ||
return []; | ||
} | ||
|
||
$extendedClassName = $node->extends->toString(); | ||
|
||
if (\in_array($extendedClassName, $this->classesAllowedToBeExtended, true)) { | ||
return []; | ||
} | ||
|
||
if (!isset($node->namespacedName)) { | ||
return [ | ||
\sprintf( | ||
'Anonymous class is not allowed to extend "%s".', | ||
$extendedClassName | ||
), | ||
]; | ||
} | ||
|
||
return [ | ||
\sprintf( | ||
'Class "%s" is not allowed to extend "%s".', | ||
$node->namespacedName, | ||
$extendedClassName | ||
), | ||
]; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
test/Fixture/Classes/NoExtendsRule/Failure/ClassExtendingOtherClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRule\Failure; | ||
|
||
final class ClassExtendingOtherClass extends OtherClass | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRule\Failure; | ||
|
||
class OtherClass | ||
{ | ||
} |
12 changes: 12 additions & 0 deletions
12
test/Fixture/Classes/NoExtendsRule/Failure/anonymous-class-extending-other-class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRule\Failure; | ||
|
||
$foo = new class() extends OtherClass { | ||
public function __toString(): string | ||
{ | ||
return 'Hmm'; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRule\Success; | ||
|
||
final class ExampleClass | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
test/Fixture/Classes/NoExtendsRule/Success/ExampleInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRule\Success; | ||
|
||
interface ExampleInterface | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
test/Fixture/Classes/NoExtendsRule/Success/InterfaceExtendingOtherInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRule\Success; | ||
|
||
interface InterfaceExtendingOtherInterface extends OtherInterface | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
test/Fixture/Classes/NoExtendsRule/Success/OtherInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRule\Success; | ||
|
||
interface OtherInterface | ||
{ | ||
} |
12 changes: 12 additions & 0 deletions
12
test/Fixture/Classes/NoExtendsRule/Success/anonymous-class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRule\Success; | ||
|
||
$foo = new class() { | ||
public function __toString(): string | ||
{ | ||
return 'Hmm'; | ||
} | ||
}; |
9 changes: 9 additions & 0 deletions
9
.../Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Failure/ClassExtendingOtherClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Failure; | ||
|
||
final class ClassExtendingOtherClass extends OtherClass | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
test/Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Failure/OtherClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Failure; | ||
|
||
class OtherClass | ||
{ | ||
} |
12 changes: 12 additions & 0 deletions
12
...tendsRuleWithClassesAllowedToBeExtended/Failure/anonymous-class-extending-other-class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Failure; | ||
|
||
$foo = new class() extends OtherClass { | ||
public function __toString(): string | ||
{ | ||
return 'Hmm'; | ||
} | ||
}; |
9 changes: 9 additions & 0 deletions
9
.../Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/ClassAllowedToBeExtended.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Success; | ||
|
||
class ClassAllowedToBeExtended | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
...endsRuleWithClassesAllowedToBeExtended/Success/ClassExtendingClassAllowedToBeExtended.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Success; | ||
|
||
final class ClassExtendingClassAllowedToBeExtended extends ClassAllowedToBeExtended | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
test/Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/ExampleClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Success; | ||
|
||
final class ExampleClass | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
.../Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/ExampleInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Success; | ||
|
||
interface ExampleInterface | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
.../NoExtendsRuleWithClassesAllowedToBeExtended/Success/InterfaceExtendingOtherInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Success; | ||
|
||
interface InterfaceExtendingOtherInterface extends OtherInterface | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
test/Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/OtherInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Success; | ||
|
||
interface OtherInterface | ||
{ | ||
} |
12 changes: 12 additions & 0 deletions
12
...sesAllowedToBeExtended/Success/anonymous-class-extending-class-allowed-to-be-extended.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Success; | ||
|
||
$foo = new class() extends ClassAllowedToBeExtended { | ||
public function __toString(): string | ||
{ | ||
return 'Hmm'; | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
test/Fixture/Classes/NoExtendsRuleWithClassesAllowedToBeExtended/Success/anonymous-class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Classes\NoExtendsRuleWithClassesAllowedToBeExtended\Success; | ||
|
||
$foo = new class() { | ||
public function __toString(): string | ||
{ | ||
return 'Hmm'; | ||
} | ||
}; |
Oops, something went wrong.