Skip to content

Commit

Permalink
define: case_insensitive is ignored, case-insensitive constants are n…
Browse files Browse the repository at this point in the history
…o longer supported
  • Loading branch information
mglaman committed Oct 15, 2021
1 parent ba16285 commit 732d033
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rules:
- PHPStan\Rules\Functions\ArrowFunctionReturnNullsafeByRefRule
- PHPStan\Rules\Functions\CallToFunctionParametersRule
- PHPStan\Rules\Functions\ClosureAttributesRule
- PHPStan\Rules\Functions\DefineParametersRule
- PHPStan\Rules\Functions\ExistingClassesInArrowFunctionTypehintsRule
- PHPStan\Rules\Functions\ExistingClassesInClosureTypehintsRule
- PHPStan\Rules\Functions\ExistingClassesInTypehintsRule
Expand Down
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,9 @@ public function supportsEnums(): bool
return $this->versionId >= 80100;
}

public function supportsCaseInsensitiveConstantNames(): bool
{
return $this->versionId < 80000;
}

}
54 changes: 54 additions & 0 deletions src/Rules/Functions/DefineParametersRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\FuncCall>
*/
class DefineParametersRule implements \PHPStan\Rules\Rule
{

private PhpVersion $phpVersion;

public function __construct(PhpVersion $phpVersion)
{
$this->phpVersion = $phpVersion;
}

public function getNodeType(): string
{
return FuncCall::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (!($node->name instanceof \PhpParser\Node\Name)) {
return [];
}
if ($this->phpVersion->supportsCaseInsensitiveConstantNames()) {
return [];
}
$name = strtolower((string) $node->name);
if ($name !== 'define') {
return [];
}
$args = $node->getArgs();
$argsCount = count($args);
// Expects 2 or 3, 1 arg is caught by CallToFunctionParametersRule
if ($argsCount < 3) {
return [];
}
return [
RuleErrorBuilder::message(
'Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported.'
)->line($node->getLine())->build(),
];
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Functions/DefineParametersRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;

/**
* @extends \PHPStan\Testing\RuleTestCase<DefineParametersRule>
*/
class DefineParametersRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): \PHPStan\Rules\Rule
{
return new DefineParametersRule(new PhpVersion(PHP_VERSION_ID));
}

public function testFile(): void
{
if (PHP_VERSION_ID < 80000) {
$this->analyse([__DIR__ . '/data/call-to-define.php'], []);
} else {
$this->analyse([__DIR__ . '/data/call-to-define.php'], [
[
'Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported.',
3,
],
]);
}
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Functions/data/call-to-define.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

define('CaSeInSeNsItIvE', 0, true);
define('CaSeInSeNsItIvE_IsOK', 0);
define('all_lowercase', 1);
define('ALL_UPPERCASE', 1);

0 comments on commit 732d033

Please sign in to comment.