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
5 changes: 4 additions & 1 deletion src/Rules/Methods/CallPrivateMethodThroughStaticRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public function processNode(Node $node, Scope $scope): array
'Unsafe call to private method %s::%s() through static::.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
))->identifier('staticClassAccess.privateMethod')->build(),
))
->line($node->name->getStartLine())
->identifier('staticClassAccess.privateMethod')
->build(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Methods/CallStaticMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array
*/
private function processSingleMethodCall(Scope $scope, StaticCall $node, string $methodName): array
{
[$errors, $method] = $this->methodCallCheck->check($scope, $methodName, $node->class);
[$errors, $method] = $this->methodCallCheck->check($scope, $methodName, $node->class, $node->name);
if ($method === null) {
return $errors;
}
Expand Down
21 changes: 17 additions & 4 deletions src/Rules/Methods/MethodCallCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ public function check(
'Cannot call method %s() on %s.',
$methodName,
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
))->identifier('method.nonObject')->build(),
))
->line($astName->getStartLine())
->identifier('method.nonObject')
->build(),
],
null,
];
Expand Down Expand Up @@ -106,7 +109,10 @@ public function check(
'Call to private method %s() of parent class %s.',
$methodReflection->getName(),
$parentClassReflection->getDisplayName(),
))->identifier('method.private')->build(),
))
->line($astName->getStartLine())
->identifier('method.private')
->build(),
],
$methodReflection,
];
Expand All @@ -133,7 +139,10 @@ public function check(
'Call to an undefined method %s::%s().',
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
$methodName,
))->identifier('method.notFound')->build(),
))
->line($astName->getStartLine())
->identifier('method.notFound')
->build(),
],
null,
];
Expand All @@ -150,6 +159,7 @@ public function check(
$methodReflection->getName(),
$declaringClass->getDisplayName(),
))
->line($astName->getStartLine())
->identifier(sprintf('method.%s', $methodReflection->isPrivate() ? 'private' : 'protected'))
->build();
}
Expand All @@ -161,7 +171,10 @@ public function check(
) {
$errors[] = RuleErrorBuilder::message(
sprintf('Call to method %s with incorrect case: %s', $messagesMethodName, $methodName),
)->identifier('method.nameCase')->build();
)
->line($astName->getStartLine())
->identifier('method.nameCase')
->build();
}

return [$errors, $methodReflection];
Expand Down
1 change: 1 addition & 0 deletions src/Rules/Methods/NullsafeMethodCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function processNode(Node $node, Scope $scope): array

return [
RuleErrorBuilder::message(sprintf('Using nullsafe method call on non-nullable type %s. Use -> instead.', $calledOnType->describe(VerbosityLevel::typeOnly())))
->line($node->name->getStartLine())
->identifier('nullsafe.neverNull')
->build(),
];
Expand Down
48 changes: 37 additions & 11 deletions src/Rules/Methods/StaticMethodCallCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DOMDocument;
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
Expand Down Expand Up @@ -61,6 +62,7 @@ public function check(
Scope $scope,
string $methodName,
$class,
Identifier|Expr $astName,
): array
{
$errors = [];
Expand All @@ -81,7 +83,9 @@ public function check(
'Calling %s::%s() outside of class scope.',
$className,
$methodName,
))->identifier(sprintf('outOfClass.%s', $lowercasedClassName))->build(),
))
->line($astName->getStartLine())
->identifier(sprintf('outOfClass.%s', $lowercasedClassName))->build(),
],
null,
];
Expand All @@ -95,7 +99,10 @@ public function check(
'Calling %s::%s() outside of class scope.',
$className,
$methodName,
))->identifier(sprintf('outOfClass.parent'))->build(),
))
->line($astName->getStartLine())
->identifier(sprintf('outOfClass.parent'))
->build(),
],
null,
];
Expand All @@ -110,7 +117,10 @@ public function check(
$scope->getFunctionName(),
$methodName,
$scope->getClassReflection()->getDisplayName(),
))->identifier('class.noParent')->build(),
))
->line($astName->getStartLine())
->identifier('class.noParent')
->build(),
],
null,
];
Expand All @@ -132,6 +142,7 @@ public function check(
$methodName,
$className,
))
->line($astName->getStartLine())
->identifier('class.notFound');

if ($this->discoveringSymbolsTip) {
Expand Down Expand Up @@ -206,7 +217,10 @@ public function check(
'Cannot call static method %s() on %s.',
$methodName,
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
))->identifier('staticMethod.nonObject')->build(),
))
->line($astName->getStartLine())
->identifier('staticMethod.nonObject')
->build(),
]),
null,
];
Expand All @@ -232,7 +246,10 @@ public function check(
'Call to an undefined static method %s::%s().',
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
$methodName,
))->identifier('staticMethod.notFound')->build(),
))
->line($astName->getStartLine())
->identifier('staticMethod.notFound')
->build(),
]),
null,
];
Expand Down Expand Up @@ -265,7 +282,10 @@ public function check(
'Static call to instance method %s::%s().',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
))->identifier('method.staticCall')->build(),
))
->line($astName->getStartLine())
->identifier('method.staticCall')
->build(),
]),
$method,
];
Expand All @@ -281,6 +301,7 @@ public function check(
$method->getName(),
$method->getDeclaringClass()->getDisplayName(),
))
->line($astName->getStartLine())
->identifier(sprintf('staticMethod.%s', $method->isPrivate() ? 'private' : 'protected'))
->build(),
]);
Expand All @@ -294,10 +315,12 @@ public function check(
$method->isStatic() ? ' static' : '',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
))->identifier(sprintf(
'%s.callToAbstract',
$method->isStatic() ? 'staticMethod' : 'method',
))->build(),
))
->line($astName->getStartLine())
->identifier(sprintf(
'%s.callToAbstract',
$method->isStatic() ? 'staticMethod' : 'method',
))->build(),
],
$method,
];
Expand All @@ -317,7 +340,10 @@ public function check(
'Call to %s with incorrect case: %s',
$lowercasedMethodName,
$methodName,
))->identifier('staticMethod.nameCase')->build();
))
->line($astName->getStartLine())
->identifier('staticMethod.nameCase')
->build();
}

return [$errors, $method];
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Methods/StaticMethodCallableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function processNode(Node $node, Scope $scope): array

$methodNameName = $methodName->toString();

[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodNameName, $node->getClass());
[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodNameName, $node->getClass(), $node->getName());
if ($methodReflection === null) {
return $errors;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Rules/Operators/PipeOperatorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public function processNode(Node $node, Scope $scope): array
RuleErrorBuilder::message(sprintf(
'Parameter #1%s of callable on the right side of pipe operator is passed by reference.',
$parameter->getName() !== '' ? ' $' . $parameter->getName() : '',
))->identifier('pipe.byRef')
))
->line($node->right->getStartLine())
->identifier('pipe.byRef')
->build(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public function processNode(Node $node, Scope $scope): array
'Unsafe access to private property %s::$%s through static::.',
$property->getDeclaringClass()->getDisplayName(),
$propertyName,
))->identifier('staticClassAccess.privateProperty')->build(),
))
->line($node->name->getStartLine())
->identifier('staticClassAccess.privateProperty')
->build(),
];
}

Expand Down
26 changes: 21 additions & 5 deletions src/Rules/Properties/AccessPropertiesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function check(PropertyFetch $node, Scope $scope, bool $write): array
$originalNameType = $scope->getType($node->name);
$className = $scope->getType($node->var)->describe(VerbosityLevel::typeOnly());
$errors[] = RuleErrorBuilder::message(sprintf('Property name for %s must be a string, but %s was given.', $className, $originalNameType->describe(VerbosityLevel::precise())))
->line($node->name->getStartLine())
->identifier('property.nameNotString')
->build();
}
Expand Down Expand Up @@ -124,7 +125,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
'Cannot access property $%s on %s.',
$name,
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
))->identifier('property.nonObject')->build(),
))
->line($node->name->getStartLine())
->identifier('property.nonObject')
->build(),
];
}

Expand Down Expand Up @@ -183,7 +187,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
'Access to private property $%s of parent class %s.',
$name,
$parentClassReflection->getDisplayName(),
))->identifier('property.private')->build(),
))
->line($node->name->getStartLine())
->identifier('property.private')
->build(),
];
}

Expand All @@ -208,7 +215,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
'Non-static access to static property %s::$%s.',
$type->getStaticProperty($name, $scope)->getDeclaringClass()->getDisplayName(),
$name,
))->identifier('staticProperty.nonStaticAccess')->build(),
))
->line($node->name->getStartLine())
->identifier('staticProperty.nonStaticAccess')
->build(),
];
}

Expand Down Expand Up @@ -255,7 +265,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
$propertyReflection->isPrivate() ? 'private' : 'protected',
$type->describe(VerbosityLevel::typeOnly()),
$name,
))->identifier(sprintf('property.%s', $propertyReflection->isPrivate() ? 'private' : 'protected'))->build(),
))
->line($node->name->getStartLine())
->identifier(sprintf('property.%s', $propertyReflection->isPrivate() ? 'private' : 'protected'))
->build(),
];
}

Expand All @@ -265,7 +278,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
$propertyReflection->isPrivateSet() ? 'private(set)' : 'protected(set)',
$type->describe(VerbosityLevel::typeOnly()),
$name,
))->identifier(sprintf('assign.property%s', $propertyReflection->isPrivateSet() ? 'PrivateSet' : 'ProtectedSet'))->build(),
))
->line($node->name->getStartLine())
->identifier(sprintf('assign.property%s', $propertyReflection->isPrivateSet() ? 'PrivateSet' : 'ProtectedSet'))
->build(),
];
}

Expand Down
Loading
Loading