-
Notifications
You must be signed in to change notification settings - Fork 510
define: case_insensitive is ignored, case-insensitive constants are no longer supported #589
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
Merged
ondrejmirtes
merged 5 commits into
phpstan:master
from
mglaman:php8-define-case-insenstivie
Oct 15, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
95236e3
PHP8 signature map for define
mglaman d42e28a
Revert changes to signature map for define call
mglaman 41a3e66
Set stub error message
mglaman 54366b7
DefineParametersRule for checking deprecated arg 3
mglaman 4dbefe4
Use RuleErrorBuilder and add method for checking case-insensitive con…
mglaman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,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
32
tests/PHPStan/Rules/Functions/DefineParametersRuleTest.php
This file contains hidden or 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,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, | ||
], | ||
]); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or 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,6 @@ | ||
<?php | ||
|
||
define('CaSeInSeNsItIvE', 0, true); | ||
mglaman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
define('CaSeInSeNsItIvE_IsOK', 0); | ||
define('all_lowercase', 1); | ||
define('ALL_UPPERCASE', 1); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes this handles PHP 8 not supporting it at all. What about handling the fact it was deprecated in 7.3
Per https://www.php.net/manual/en/function.define.php,
Should we add another method that checks the PHP version ID if they're deprecated so we can report that error in our rule? Or just not supported and that's it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reporting something on 7.3 and below 8.0 is somewhat a different scenario that's a better fit for https://github.com/phpstan/phpstan-deprecation-rules.