Skip to content

Commit

Permalink
Bleeding edge - check classes extending @final classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 20, 2021
1 parent c5f9d24 commit dffd2c2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ parameters:
neverInGenericReturnType: true
validateOverridingMethodsInStubs: true
crossCheckInterfaces: true
finalByPhpDocTag: true
stubFiles:
- ../stubs/arrayFunctions.stub
8 changes: 7 additions & 1 deletion conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ rules:
- PHPStan\Rules\Classes\DuplicateDeclarationRule
- PHPStan\Rules\Classes\ExistingClassesInClassImplementsRule
- PHPStan\Rules\Classes\ExistingClassesInInterfaceExtendsRule
- PHPStan\Rules\Classes\ExistingClassInClassExtendsRule
- PHPStan\Rules\Classes\ExistingClassInTraitUseRule
- PHPStan\Rules\Classes\InstantiationRule
- PHPStan\Rules\Classes\InvalidPromotedPropertiesRule
Expand Down Expand Up @@ -98,6 +97,13 @@ services:
-
class: PHPStan\Rules\Api\PhpStanNamespaceIn3rdPartyPackageRule

-
class: PHPStan\Rules\Classes\ExistingClassInClassExtendsRule
arguments:
checkFinalByPhpDocTag: %featureToggles.finalByPhpDocTag%
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\Classes\ExistingClassInInstanceOfRule
tags:
Expand Down
4 changes: 3 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ parameters:
neverInGenericReturnType: false
validateOverridingMethodsInStubs: false
crossCheckInterfaces: false
finalByPhpDocTag: false
fileExtensions:
- php
checkAlwaysTrueCheckTypeFunctionCall: false
Expand Down Expand Up @@ -219,7 +220,8 @@ parametersSchema:
deepInspectTypes: bool(),
neverInGenericReturnType: bool(),
validateOverridingMethodsInStubs: bool(),
crossCheckInterfaces: bool()
crossCheckInterfaces: bool(),
finalByPhpDocTag: bool()
])
fileExtensions: listOf(string())
checkAlwaysTrueCheckTypeFunctionCall: bool()
Expand Down
12 changes: 11 additions & 1 deletion src/Rules/Classes/ExistingClassInClassExtendsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ class ExistingClassInClassExtendsRule implements \PHPStan\Rules\Rule

private ReflectionProvider $reflectionProvider;

private bool $checkFinalByPhpDocTag;

public function __construct(
ClassCaseSensitivityCheck $classCaseSensitivityCheck,
ReflectionProvider $reflectionProvider
ReflectionProvider $reflectionProvider,
bool $checkFinalByPhpDocTag = false
)
{
$this->classCaseSensitivityCheck = $classCaseSensitivityCheck;
$this->reflectionProvider = $reflectionProvider;
$this->checkFinalByPhpDocTag = $checkFinalByPhpDocTag;
}

public function getNodeType(): string
Expand Down Expand Up @@ -72,6 +76,12 @@ public function processNode(Node $node, Scope $scope): array
$currentClassName !== null ? sprintf('Class %s', $currentClassName) : 'Anonymous class',
$extendedClassName
))->nonIgnorable()->build();
} elseif ($this->checkFinalByPhpDocTag && $reflection->isFinal()) {
$messages[] = RuleErrorBuilder::message(sprintf(
'%s extends @final class %s.',
$currentClassName !== null ? sprintf('Class %s', $currentClassName) : 'Anonymous class',
$extendedClassName
))->build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ protected function getRule(): Rule
$broker = $this->createReflectionProvider();
return new ExistingClassInClassExtendsRule(
new ClassCaseSensitivityCheck($broker),
$broker
$broker,
true
);
}

Expand All @@ -27,6 +28,10 @@ public function testRule(): void
'Class ExtendsImplements\Foo referenced with incorrect case: ExtendsImplements\FOO.',
15,
],
[
'Class ExtendsImplements\ExtendsFinalWithAnnotation extends @final class ExtendsImplements\FinalWithAnnotation.',
43,
],
]);
}

Expand Down Expand Up @@ -61,4 +66,14 @@ public function testRuleExtendsError(): void
]);
}

public function testFinalByTag(): void
{
$this->analyse([__DIR__ . '/data/extends-final-by-tag.php'], [
[
'Class ExtendsFinalByTag\Bar2 extends @final class ExtendsFinalByTag\Bar.',
21,
],
]);
}

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Classes/data/extends-final-by-tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ExtendsFinalByTag;

class Foo
{

}

/** @final */
class Bar
{

}

class Foo2 extends Foo
{

}

class Bar2 extends Bar
{

}

0 comments on commit dffd2c2

Please sign in to comment.