forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MethodCall: mark virtual nullsafe call with attribute
- Loading branch information
Showing
5 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
56 changes: 56 additions & 0 deletions
56
tests/PHPStan/Rules/Methods/VirtualNullsafeMethodCallTest.php
This file contains 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,56 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<Rule> | ||
*/ | ||
class VirtualNullsafeMethodCallTest extends RuleTestCase | ||
{ | ||
|
||
/** | ||
* @return Rule<MethodCall> | ||
*/ | ||
protected function getRule(): Rule | ||
{ | ||
return new /** @implements Rule<MethodCall> */ class implements Rule { | ||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($node->getAttribute('virtualNullsafeMethodCall') === true) { | ||
return [RuleErrorBuilder::message('Nullable method call detected')->identifier('')->build()]; | ||
} | ||
|
||
return [RuleErrorBuilder::message('Regular method call detected')->identifier('')->build()]; | ||
} | ||
|
||
}; | ||
} | ||
|
||
public function testAttribute(): void | ||
{ | ||
$this->analyse([ __DIR__ . '/data/virtual-nullsafe-method-call.php'], [ | ||
[ | ||
'Regular method call detected', | ||
3, | ||
], | ||
[ | ||
'Nullable method call detected', | ||
4, | ||
], | ||
]); | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
tests/PHPStan/Rules/Methods/data/virtual-nullsafe-method-call.php
This file contains 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,4 @@ | ||
<?php // lint >= 8.0 | ||
|
||
$foo->regularCall(); | ||
$foo?->nullsafeCall(); |
56 changes: 56 additions & 0 deletions
56
tests/PHPStan/Rules/Properties/VirtualNullsafePropertyFetchTest.php
This file contains 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,56 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Properties; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<Rule> | ||
*/ | ||
class VirtualNullsafePropertyFetchTest extends RuleTestCase | ||
{ | ||
|
||
/** | ||
* @return Rule<PropertyFetch> | ||
*/ | ||
protected function getRule(): Rule | ||
{ | ||
return new /** @implements Rule<PropertyFetch> */ class implements Rule { | ||
|
||
public function getNodeType(): string | ||
{ | ||
return PropertyFetch::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($node->getAttribute('virtualNullsafePropertyFetch') === true) { | ||
return [RuleErrorBuilder::message('Nullable property fetch detected')->identifier('')->build()]; | ||
} | ||
|
||
return [RuleErrorBuilder::message('Regular property fetch detected')->identifier('')->build()]; | ||
} | ||
|
||
}; | ||
} | ||
|
||
public function testAttribute(): void | ||
{ | ||
$this->analyse([ __DIR__ . '/data/virtual-nullsafe-property-fetch.php'], [ | ||
[ | ||
'Regular property fetch detected', | ||
3, | ||
], | ||
[ | ||
'Nullable property fetch detected', | ||
4, | ||
], | ||
]); | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
tests/PHPStan/Rules/Properties/data/virtual-nullsafe-property-fetch.php
This file contains 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,4 @@ | ||
<?php // lint >= 8.0 | ||
|
||
$foo->regularFetch; | ||
$foo?->nullsafeFetch; |