Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dynamic class constant fetch available in PHP 8.3 #242

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
run: composer lint
- php-version: "8.1"
run: composer lint
- php-version: "8.2"
run: composer lint
include:
- php-version: "7.2"
run: composer lint-7.x
Expand All @@ -42,6 +44,8 @@ jobs:
run: composer lint-8.0
- php-version: "8.1"
run: composer lint-8.1
- php-version: "8.2"
run: composer lint-8.2

steps:
- uses: actions/checkout@v4
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
},
"scripts": {
"lint": "vendor/bin/parallel-lint --colors src/ tests/",
"lint-7.x": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/src/TypesEverywhere.php --exclude tests/src/AttributesEverywhere.php --exclude tests/src/disallowed/functionCallsNamedParams.php --exclude tests/src/disallowed-allow/functionCallsNamedParams.php --exclude tests/src/disallowed/attributeUsages.php --exclude tests/src/disallowed-allow/attributeUsages.php",
"lint-8.0": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/src/TypesEverywhere.php --exclude tests/src/AttributesEverywhere.php",
"lint-8.1": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/src/AttributesEverywhere.php",
"lint-7.x": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/src/TypesEverywhere.php --exclude tests/src/AttributesEverywhere.php --exclude tests/src/disallowed/functionCallsNamedParams.php --exclude tests/src/disallowed-allow/functionCallsNamedParams.php --exclude tests/src/disallowed/attributeUsages.php --exclude tests/src/disallowed-allow/attributeUsages.php --exclude tests/src/disallowed/constantDynamicUsages.php --exclude tests/src/disallowed-allow/constantDynamicUsages.php",
"lint-8.0": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/src/TypesEverywhere.php --exclude tests/src/AttributesEverywhere.php --exclude tests/src/disallowed/constantDynamicUsages.php --exclude tests/src/disallowed-allow/constantDynamicUsages.php",
"lint-8.1": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/src/AttributesEverywhere.php --exclude tests/src/disallowed/constantDynamicUsages.php --exclude tests/src/disallowed-allow/constantDynamicUsages.php",
"lint-8.2": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/src/disallowed/constantDynamicUsages.php --exclude tests/src/disallowed-allow/constantDynamicUsages.php",
"lint-neon": "vendor/bin/neon-lint .",
"phpcs": "vendor/bin/phpcs src/ tests/",
"cs-fix": "vendor/bin/phpcbf src/ tests/",
Expand Down
33 changes: 28 additions & 5 deletions src/Usages/ClassConstantUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use Spaze\PHPStan\Rules\Disallowed\DisallowedConstant;
use Spaze\PHPStan\Rules\Disallowed\DisallowedConstantFactory;
Expand Down Expand Up @@ -80,17 +82,38 @@ public function processNode(Node $node, Scope $scope): array
if (!($node instanceof ClassConstFetch)) {
throw new ShouldNotHappenException(sprintf('$node should be %s but is %s', ClassConstFetch::class, get_class($node)));
}
if (!($node->name instanceof Identifier)) {
throw new ShouldNotHappenException(sprintf('$node->name should be %s but is %s', Identifier::class, get_class($node->name)));
if ($node->name instanceof Identifier) {
return $this->getConstantRuleErrors($scope, (string)$node->name, $this->typeResolver->getType($node->class, $scope));
}
$constant = (string)$node->name;
$type = $this->typeResolver->getType($node->class, $scope);
$usedOnType = $type->getObjectTypeOrClassStringObjectType();
if ($node->name instanceof Variable) {
$type = $scope->getType($node->name);
$errors = [];
foreach ($type->getConstantStrings() as $constantString) {
$errors = array_merge(
$errors,
$this->getConstantRuleErrors($scope, $constantString->getValue(), $this->typeResolver->getType($node->class, $scope))
);
}
return $errors;
}
throw new ShouldNotHappenException(sprintf('$node->name should be %s but is %s', Identifier::class, get_class($node->name)));
}


/**
* @param Scope $scope
* @param string $constant
* @param Type $type
* @return list<RuleError>
* @throws ShouldNotHappenException
*/
private function getConstantRuleErrors(Scope $scope, string $constant, Type $type): array
{
if (strtolower($constant) === 'class') {
return [];
}

$usedOnType = $type->getObjectTypeOrClassStringObjectType();
$displayName = $usedOnType->getObjectClassNames() ? $this->getFullyQualified($usedOnType->getObjectClassNames(), $constant) : null;
if ($usedOnType->getConstantStrings()) {
$classNames = array_map(
Expand Down
21 changes: 21 additions & 0 deletions tests/Usages/ClassConstantUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@ public function testRule(): void
}


public function testRuleDynamicClassConstantFetch(): void
{
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../src/disallowed/constantDynamicUsages.php'], [
[
'Using Waldo\Quux\Blade::RUNNER is forbidden, not a replicant.',
8,
],
[
'Using Waldo\Quux\Blade::DECKARD is forbidden, maybe a replicant.',
10,
],
[
'Using Waldo\Quux\Blade::RUNNER is forbidden, not a replicant.',
10,
],
]);
$this->analyse([__DIR__ . '/../src/disallowed-allow/constantDynamicUsages.php'], []);
}


public static function getAdditionalConfigFiles(): array
{
return [
Expand Down
2 changes: 2 additions & 0 deletions tests/src/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Blade

public const WESLEY = 'Snipes';

public const MOVIE = Blade::WESLEY;


public function runner(int $everything = 0, bool $yes = false, string $roland = '303'): void
{
Expand Down
10 changes: 10 additions & 0 deletions tests/src/disallowed-allow/constantDynamicUsages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types = 1);

use Waldo\Quux\Blade;

// allowed by path
$kind = 'RUNNER';
echo Blade::{$kind};
/** @var 'DECKARD'|'MOVIE'|'RUNNER' $kind2 */
echo Blade::{$kind2};
10 changes: 10 additions & 0 deletions tests/src/disallowed/constantDynamicUsages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types = 1);

use Waldo\Quux\Blade;

// disallowed
$kind = 'RUNNER';
echo Blade::{$kind};
/** @var 'DECKARD'|'MOVIE'|'RUNNER' $kind2 */
echo Blade::{$kind2};