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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"hoa/compiler": "3.17.08.08",
"hoa/exception": "^1.0",
"hoa/file": "1.17.07.11",
"jetbrains/phpstorm-stubs": "dev-master#bdc8acd7c04c0c87197849c7cdd27e44b67b56c7",
"jetbrains/phpstorm-stubs": "dev-master#5686f9ceebde3d9338bea53b78d70ebde5fb5710",
"nette/bootstrap": "^3.0",
"nette/di": "^3.1.4",
"nette/neon": "3.3.3",
Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\NoopExpressionNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NeverType;
use function sprintf;

/**
* @implements Rule<Node\Stmt\Expression>
* @implements Rule<NoopExpressionNode>
*/
final class CallToConstructorStatementWithoutSideEffectsRule implements Rule
{
Expand All @@ -25,16 +26,16 @@ public function __construct(

public function getNodeType(): string
{
return Node\Stmt\Expression::class;
return NoopExpressionNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (!$node->expr instanceof Node\Expr\New_) {
$instantiation = $node->getOriginalExpr();
if (!$instantiation instanceof Node\Expr\New_) {
return [];
}

$instantiation = $node->expr;
if (!$instantiation->class instanceof Node\Name) {
return [];
}
Expand All @@ -59,27 +60,18 @@ public function processNode(Node $node, Scope $scope): array
}

$constructor = $classReflection->getConstructor();
if ($constructor->hasSideEffects()->no()) {
$throwsType = $constructor->getThrowType();
if ($throwsType !== null && !$throwsType->isVoid()->yes()) {
return [];
}

$methodResult = $scope->getType($instantiation);
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
return [];
}

return [
RuleErrorBuilder::message(sprintf(
'Call to %s::%s() on a separate line has no effect.',
$classReflection->getDisplayName(),
$constructor->getName(),
))->identifier('new.resultUnused')->build(),
];
$methodResult = $scope->getType($instantiation);
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
return [];
}

return [];
return [
RuleErrorBuilder::message(sprintf(
'Call to %s::%s() on a separate line has no effect.',
$classReflection->getDisplayName(),
$constructor->getName(),
))->identifier('new.resultUnused')->build(),
];
}

}
49 changes: 20 additions & 29 deletions src/Rules/Methods/CallToMethodStatementWithoutSideEffectsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\Node\NoopExpressionNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
Expand All @@ -14,7 +15,7 @@
use function sprintf;

/**
* @implements Rule<Node\Stmt\Expression>
* @implements Rule<NoopExpressionNode>
*/
final class CallToMethodStatementWithoutSideEffectsRule implements Rule
{
Expand All @@ -25,18 +26,18 @@ public function __construct(private RuleLevelHelper $ruleLevelHelper)

public function getNodeType(): string
{
return Node\Stmt\Expression::class;
return NoopExpressionNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
if ($node->expr instanceof Node\Expr\NullsafeMethodCall) {
$scope = $scope->filterByTruthyValue(new Node\Expr\BinaryOp\NotIdentical($node->expr->var, new Node\Expr\ConstFetch(new Node\Name('null'))));
} elseif (!$node->expr instanceof Node\Expr\MethodCall) {
$methodCall = $node->getOriginalExpr();
if ($methodCall instanceof Node\Expr\NullsafeMethodCall) {
$scope = $scope->filterByTruthyValue(new Node\Expr\BinaryOp\NotIdentical($methodCall->var, new Node\Expr\ConstFetch(new Node\Name('null'))));
} elseif (!$methodCall instanceof Node\Expr\MethodCall) {
return [];
}

$methodCall = $node->expr;
if (!$methodCall->name instanceof Node\Identifier) {
return [];
}
Expand All @@ -60,31 +61,21 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$method = $calledOnType->getMethod($methodName, $scope);
if ($method->hasSideEffects()->no() || $node->expr->isFirstClassCallable()) {
if (!$node->expr->isFirstClassCallable()) {
$throwsType = $method->getThrowType();
if ($throwsType !== null && !$throwsType->isVoid()->yes()) {
return [];
}
}

$methodResult = $scope->getType($methodCall);
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
return [];
}

return [
RuleErrorBuilder::message(sprintf(
'Call to %s %s::%s() on a separate line has no effect.',
$method->isStatic() ? 'static method' : 'method',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
))->identifier('method.resultUnused')->build(),
];
$methodResult = $scope->getType($methodCall);
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
return [];
}

return [];
$method = $calledOnType->getMethod($methodName, $scope);

return [
RuleErrorBuilder::message(sprintf(
'Call to %s %s::%s() on a separate line has no effect.',
$method->isStatic() ? 'static method' : 'method',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
))->identifier('method.resultUnused')->build(),
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\Node\NoopExpressionNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -17,7 +18,7 @@
use function strtolower;

/**
* @implements Rule<Node\Stmt\Expression>
* @implements Rule<NoopExpressionNode>
*/
final class CallToStaticMethodStatementWithoutSideEffectsRule implements Rule
{
Expand All @@ -31,16 +32,16 @@ public function __construct(

public function getNodeType(): string
{
return Node\Stmt\Expression::class;
return NoopExpressionNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (!$node->expr instanceof Node\Expr\StaticCall) {
$staticCall = $node->getOriginalExpr();
if (!$staticCall instanceof Node\Expr\StaticCall) {
return [];
}

$staticCall = $node->expr;
if (!$staticCall->name instanceof Node\Identifier) {
return [];
}
Expand Down Expand Up @@ -84,30 +85,19 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ($method->hasSideEffects()->no() || $node->expr->isFirstClassCallable()) {
if (!$node->expr->isFirstClassCallable()) {
$throwsType = $method->getThrowType();
if ($throwsType !== null && !$throwsType->isVoid()->yes()) {
return [];
}
}

$methodResult = $scope->getType($staticCall);
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
return [];
}

return [
RuleErrorBuilder::message(sprintf(
'Call to %s %s::%s() on a separate line has no effect.',
$method->isStatic() ? 'static method' : 'method',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
))->identifier('staticMethod.resultUnused')->build(),
];
$methodResult = $scope->getType($staticCall);
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
return [];
}

return [];
return [
RuleErrorBuilder::message(sprintf(
'Call to %s %s::%s() on a separate line has no effect.',
$method->isStatic() ? 'static method' : 'method',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
))->identifier('staticMethod.resultUnused')->build(),
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public function testRule(): void
'Call to static method DateTimeImmutable::createFromFormat() on a separate line has no effect.',
12,
],
[
'Call to static method DateTimeImmutable::createFromFormat() on a separate line has no effect.',
13,
],
[
'Call to method DateTime::format() on a separate line has no effect.',
23,
Expand Down
Loading