Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Analyser/ArgumentsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ public static function reorderNewArguments(
* The argument bound to the parameter at $position named $name: the
* positional argument at that index or the one passed by that name.
*
* This method can be useful in simple cases when no reflection is needed.
* In more complex cases where reflection information is available,
* use ParametersAcceptorSelector::selectFromArgs() and ArgumentsNormalizer::reorder*Arguments() instead.
*
* @param Arg[] $args
* @param 0|positive-int $position
* @param non-empty-string $name
Expand Down
6 changes: 4 additions & 2 deletions src/Rules/Api/NodeConnectingVisitorAttributesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
Expand Down Expand Up @@ -36,7 +37,8 @@ public function processNode(Node $node, Scope $scope): array
}

$args = $node->getArgs();
if (!isset($args[0])) {
$keyArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'key');
if ($keyArg === null) {
return [];
}

Expand All @@ -46,7 +48,7 @@ public function processNode(Node $node, Scope $scope): array
}

$messages = [];
$argType = $scope->getType($args[0]->value);
$argType = $scope->getType($keyArg->value);
foreach ($argType->getConstantStrings() as $constantString) {
$argValue = $constantString->getValue();
if (!in_array($argValue, ['parent', 'previous', 'next'], true)) {
Expand Down
11 changes: 9 additions & 2 deletions src/Rules/Constants/ValueAssignedToDefineRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\ConstantResolver;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\Conditional;
Expand Down Expand Up @@ -49,12 +50,18 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$constantNameStrings = $scope->getType($args[0]->value)->getConstantStrings();
$constantNameArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'constant_name');
$valueArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'value');
if ($constantNameArg === null || $valueArg === null) {
return [];
}

$constantNameStrings = $scope->getType($constantNameArg->value)->getConstantStrings();
if (count($constantNameStrings) === 0) {
return [];
}

$valueType = $scope->getType($args[1]->value);
$valueType = $scope->getType($valueArg->value);
$errors = [];

foreach ($constantNameStrings as $constantNameString) {
Expand Down
11 changes: 9 additions & 2 deletions src/Rules/DateIntervalInstantiationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateInterval;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\Conditional;
use PHPStan\DependencyInjection\RegisteredRule;
Expand Down Expand Up @@ -37,14 +38,20 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$args = $node->getArgs();
if (
count($node->getArgs()) === 0
count($args) === 0
|| strtolower((string) $node->class) !== 'dateinterval'
) {
return [];
}

$arg = $scope->getType($node->getArgs()[0]->value);
$arg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'duration');
if ($arg === null) {
return [];
}

$arg = $scope->getType($arg->value);
$errors = [];

foreach ($arg->getConstantStrings() as $constantString) {
Expand Down
11 changes: 9 additions & 2 deletions src/Rules/DateTimeInstantiationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use Throwable;
Expand Down Expand Up @@ -35,14 +36,20 @@ public function processNode(Node $node, Scope $scope): array
}

$lowerClassName = strtolower((string) $node->class);
$args = $node->getArgs();
if (
count($node->getArgs()) === 0
count($args) === 0
|| !in_array($lowerClassName, ['datetime', 'datetimeimmutable'], true)
) {
return [];
}

$arg = $scope->getType($node->getArgs()[0]->value);
$arg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'datetime');
if ($arg === null) {
return [];
}

$arg = $scope->getType($arg->value);
$errors = [];

foreach ($arg->getConstantStrings() as $constantString) {
Expand Down
56 changes: 37 additions & 19 deletions src/Rules/Debug/FileAssertRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\PhpDoc\TypeStringResolver;
Expand Down Expand Up @@ -46,25 +47,42 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
return [];
}

$function = $this->reflectionProvider->getFunction($node->name, $scope);
if ($function->getName() === 'PHPStan\\Testing\\assertType') {
return $this->processAssertType($node->getArgs(), $scope);
}

if ($function->getName() === 'PHPStan\\Testing\\assertNativeType') {
return $this->processAssertNativeType($node->getArgs(), $scope);
}

if ($function->getName() === 'PHPStan\\Testing\\assertSuperType') {
return $this->processAssertSuperType($node->getArgs(), $scope);
}

if ($function->getName() === 'PHPStan\\Testing\\assertVariableCertainty') {
return $this->processAssertVariableCertainty($node->getArgs(), $scope);
$name = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
$args = $node->getArgs();
if ($name === 'PHPStan\\Testing\\assertType') {
$typeArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'type');
$valueArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'value');
if ($typeArg === null || $valueArg === null) {
return [];
}
return $this->processAssertType([$typeArg, $valueArg], $scope);
}

if ($name === 'PHPStan\\Testing\\assertNativeType') {
$typeArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'type');
$valueArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'value');
if ($typeArg === null || $valueArg === null) {
return [];
}
return $this->processAssertNativeType([$typeArg, $valueArg], $scope);
}

if ($name === 'PHPStan\\Testing\\assertSuperType') {
$typeArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'superType');
$valueArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'value');
if ($typeArg === null || $valueArg === null) {
return [];
}
return $this->processAssertSuperType([$typeArg, $valueArg], $scope);
}

if ($name === 'PHPStan\\Testing\\assertVariableCertainty') {
$certaintyArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'certainty');
$variableArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'variable');
if ($certaintyArg === null || $variableArg === null) {
return [];
}
return $this->processAssertVariableCertainty([$certaintyArg, $variableArg], $scope);
}

return [];
Expand Down
23 changes: 12 additions & 11 deletions src/Rules/Functions/PrintfArrayParametersRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -46,23 +47,22 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
return [];
}

$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
$name = $functionReflection->getName();
$name = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
if (!in_array($name, ['vprintf', 'vsprintf'], true)) {
return [];
}

$args = $node->getArgs();
$argsCount = count($args);
if ($argsCount < 1) {
if (count($args) < 1) {
return []; // caught by CallToFunctionParametersRule
}

$formatArgType = $scope->getType($args[0]->value);
$formatArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'format');
if ($formatArg === null) {
return [];
}

$formatArgType = $scope->getType($formatArg->value);
$placeHoldersCounts = [];
foreach ($formatArgType->getConstantStrings() as $formatString) {
$format = $formatString->getValue();
Expand Down Expand Up @@ -97,8 +97,9 @@ public function processNode(Node $node, Scope $scope): array
}

$formatArgsCounts = [];
if (isset($args[1])) {
$formatArgsType = $scope->getType($args[1]->value);
$formatValuesArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'values');
if ($formatValuesArg !== null) {
$formatArgsType = $scope->getType($formatValuesArg->value);

$constantArrays = $formatArgsType->getConstantArrays();
if ($constantArrays === []) {
Expand Down
15 changes: 11 additions & 4 deletions src/Rules/Functions/RandomIntParametersRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
Expand Down Expand Up @@ -44,17 +45,23 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$args = $node->getArgs();
if (count($args) < 2) {
return [];
}

if ($this->reflectionProvider->resolveFunctionName($node->name, $scope) !== 'random_int') {
return [];
}

$args = $node->getArgs();
if (count($args) < 2) {
$minArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'min');
$maxArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'max');
if ($minArg === null || $maxArg === null) {
return [];
}

$minType = $scope->getType($args[0]->value)->toInteger();
$maxType = $scope->getType($args[1]->value)->toInteger();
$minType = $scope->getType($minArg->value)->toInteger();
$maxType = $scope->getType($maxArg->value)->toInteger();

if (
!$minType instanceof ConstantIntegerType && !$minType instanceof IntegerRangeType
Expand Down
19 changes: 16 additions & 3 deletions src/Rules/Regexp/RegularExpressionPatternRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
Expand Down Expand Up @@ -36,6 +37,15 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Name) {
return [];
}

$functionName = strtolower((string) $node->name);
if (!str_starts_with($functionName, 'preg_')) {
return [];
}

$patterns = $this->extractPatterns($node, $scope);

$errors = [];
Expand All @@ -60,14 +70,17 @@ private function extractPatterns(FuncCall $functionCall, Scope $scope): array
return [];
}
$functionName = strtolower((string) $functionCall->name);
if (!str_starts_with($functionName, 'preg_')) {

$args = $functionCall->getArgs();
if (!isset($args[0])) {
return [];
}

if (!isset($functionCall->getArgs()[0])) {
$patternArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'pattern');
if ($patternArg === null) {
return [];
}
$patternNode = $functionCall->getArgs()[0]->value;
$patternNode = $patternArg->value;
$patternType = $scope->getType($patternNode);

$patternStrings = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public function testRule(): void
24,
'See: https://phpstan.org/blog/preprocessing-ast-for-custom-rules',
],
[
'Node attribute \'parent\' is no longer available.',
25,
'See: https://phpstan.org/blog/preprocessing-ast-for-custom-rules',
],
]);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/PHPStan/Rules/Api/data/node-connecting-visitor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // lint >= 8.0

namespace NodeConnectingVisitorRule;

Expand All @@ -22,6 +22,7 @@ public function processNode(Node $node, Scope $scope): array
$parent = $node->getAttribute("parent");
$custom = $node->getAttribute("myCustomAttribute");
$parent = $node->getAttribute($this->attrName);
$namedArgs = $node->getAttribute(default: null, key: 'parent');

return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function testRule(): void
'Configuration defined type for constant A_NON_EMPTY_STRING (non-empty-string) does not accept value string.',
14,
],
[
'Configuration defined type for constant BAR_CONSTANT (int|string|null) does not accept value false.',
19,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php // lint >= 8.0

namespace ValueAssignedToDefine;

Expand All @@ -15,3 +15,5 @@
define('A_NON_EMPTY_STRING', dirname(__DIR__, 2));

function getString(): string {}

define(value: false, constant_name: 'BAR_CONSTANT'); // error
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/DateIntervalInstantiationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function test(): void
'Instantiating DateInterval with invalid produces an error: ' . $prefix . 'Unknown or bad format (invalid)',
37,
],
[
'Instantiating DateInterval with 1M produces an error: ' . $prefix . 'Unknown or bad format (1M)',
40,
],
],
);
}
Expand Down
Loading
Loading