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
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ parameters:
disableRuntimeReflectionProvider: true
illegalConstructorMethodCall: true
disableCheckMissingIterableValueType: true
strictUnnecessaryNullsafePropertyFetch: true
8 changes: 7 additions & 1 deletion conf/config.level4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ rules:
- PHPStan\Rules\Methods\CallToMethodStatementWithoutSideEffectsRule
- PHPStan\Rules\Methods\CallToStaticMethodStatementWithoutSideEffectsRule
- PHPStan\Rules\Methods\NullsafeMethodCallRule
- PHPStan\Rules\Properties\NullsafePropertyFetchRule
- PHPStan\Rules\TooWideTypehints\TooWideArrowFunctionReturnTypehintRule
- PHPStan\Rules\TooWideTypehints\TooWideClosureReturnTypehintRule
- PHPStan\Rules\TooWideTypehints\TooWideFunctionReturnTypehintRule
Expand Down Expand Up @@ -162,3 +161,10 @@ services:
checkProtectedAndPublicMethods: %checkTooWideReturnTypesInProtectedAndPublicMethods%
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\Properties\NullsafePropertyFetchRule
arguments:
strictUnnecessaryNullsafePropertyFetch: %featureToggles.strictUnnecessaryNullsafePropertyFetch%
tags:
- phpstan.rules.rule
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ parameters:
nodeConnectingVisitorRule: false
illegalConstructorMethodCall: false
disableCheckMissingIterableValueType: false
strictUnnecessaryNullsafePropertyFetch: false
fileExtensions:
- php
checkAdvancedIsset: false
Expand Down Expand Up @@ -226,6 +227,7 @@ parametersSchema:
nodeConnectingVisitorRule: bool(),
illegalConstructorMethodCall: bool(),
disableCheckMissingIterableValueType: bool(),
strictUnnecessaryNullsafePropertyFetch: bool(),
])
fileExtensions: listOf(string())
checkAdvancedIsset: bool()
Expand Down
14 changes: 14 additions & 0 deletions src/Rules/Properties/NullsafePropertyFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
class NullsafePropertyFetchRule implements Rule
{

public function __construct(private bool $strictUnnecessaryNullsafePropertyFetch)
{
}

public function getNodeType(): string
{
return Node\Expr\NullsafePropertyFetch::class;
Expand All @@ -33,6 +37,16 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ($scope->isUndefinedExpressionAllowed($node)) {
if ($this->strictUnnecessaryNullsafePropertyFetch) {
return [
RuleErrorBuilder::message('Using nullsafe property access on left side of ?? / in isset / in empty is unnecessary. Use -> instead.')->build(),
];
}

return [];
}

return [
RuleErrorBuilder::message(sprintf('Using nullsafe property access on non-nullable type %s. Use -> instead.', $calledOnType->describe(VerbosityLevel::typeOnly())))->build(),
];
Expand Down
43 changes: 42 additions & 1 deletion tests/PHPStan/Rules/Properties/NullsafePropertyFetchRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
class NullsafePropertyFetchRuleTest extends RuleTestCase
{

private bool $strictUnnecessaryNullsafePropertyFetch;

protected function getRule(): Rule
{
return new NullsafePropertyFetchRule();
return new NullsafePropertyFetchRule($this->strictUnnecessaryNullsafePropertyFetch);
}

public function testRule(): void
{
$this->strictUnnecessaryNullsafePropertyFetch = false;

if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 8.0.');
}
Expand All @@ -33,11 +37,48 @@ public function testRule(): void

public function testBug6020(): void
{
$this->strictUnnecessaryNullsafePropertyFetch = false;

if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/bug-6020.php'], []);
}

public function testBug7109(): void
{
$this->strictUnnecessaryNullsafePropertyFetch = false;

if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/bug-7109.php'], []);
}

public function testBug7109Strict(): void
{
$this->strictUnnecessaryNullsafePropertyFetch = true;

if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/bug-7109.php'], [
[
'Using nullsafe property access on left side of ?? / in isset / in empty is unnecessary. Use -> instead.',
15,
],
[
'Using nullsafe property access on left side of ?? / in isset / in empty is unnecessary. Use -> instead.',
16,
],
[
'Using nullsafe property access on left side of ?? / in isset / in empty is unnecessary. Use -> instead.',
17,
],
]);
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-7109.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1); // lint >= 8.0

class HelloWorld
{
public int $aaa = 5;
/**
* @return HelloWorld|null
*/
public function get(): ?HelloWorld
{
return rand() ? $this : null;
}
public function sayHello(): void
{
$this->get()?->aaa ?? 6;
isset($this->get()?->aaa) ?: 6;
empty($this->get()?->aaa) ?: 6;
}
}