Skip to content
Open
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: 5 additions & 0 deletions src/Rules/IssetCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ static function (Type $type) use ($typeMessageCallback): ?string {
}

if ($expr instanceof Expr\NullsafePropertyFetch) {
$calledOnType = $this->treatPhpDocTypesAsCertain ? $scope->getScopeType($expr->var) : $scope->getScopeNativeType($expr->var);
if (!$calledOnType->isNull()->no()) {
return null;
}

if ($expr->name instanceof Node\Identifier) {
return RuleErrorBuilder::message(sprintf('Using nullsafe property access "?->%s" %s is unnecessary. Use -> instead.', $expr->name->name, $operatorDescription))
->identifier('nullsafe.neverNull')
Expand Down
16 changes: 0 additions & 16 deletions tests/PHPStan/Rules/Variables/EmptyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,14 @@ public function testBug7109(): void
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/../Properties/data/bug-7109.php'], [
[
'Using nullsafe property access "?->aaa" in empty() is unnecessary. Use -> instead.',
19,
],
[
'Using nullsafe property access "?->aaa" in empty() is unnecessary. Use -> instead.',
30,
],
[
'Using nullsafe property access "?->aaa" in empty() is unnecessary. Use -> instead.',
42,
],
[
'Using nullsafe property access "?->notFalsy" in empty() is unnecessary. Use -> instead.',
54,
],
[
'Expression in empty() is not falsy.',
59,
],
[
'Using nullsafe property access "?->aaa" in empty() is unnecessary. Use -> instead.',
68,
],
[
'Using nullsafe property access "?->(Expression)" in empty() is unnecessary. Use -> instead.',
75,
Expand Down
12 changes: 0 additions & 12 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,22 +333,10 @@ public function testBug7109(): void
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/../Properties/data/bug-7109.php'], [
[
'Using nullsafe property access "?->aaa" in isset() is unnecessary. Use -> instead.',
18,
],
[
'Using nullsafe property access "?->aaa" in isset() is unnecessary. Use -> instead.',
29,
],
[
'Expression in isset() is not nullable.',
41,
],
[
'Using nullsafe property access "?->aaa" in isset() is unnecessary. Use -> instead.',
67,
],
[
'Expression in isset() is not nullable.',
74,
Expand Down
18 changes: 6 additions & 12 deletions tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,22 +253,10 @@ public function testBug5933(): void
public function testBug7109(): void
{
$this->analyse([__DIR__ . '/../Properties/data/bug-7109.php'], [
[
'Using nullsafe property access "?->aaa" on left side of ?? is unnecessary. Use -> instead.',
17,
],
[
'Using nullsafe property access "?->aaa" on left side of ?? is unnecessary. Use -> instead.',
28,
],
[
'Expression on left side of ?? is not nullable.',
40,
],
[
'Using nullsafe property access "?->aaa" on left side of ?? is unnecessary. Use -> instead.',
66,
],
[
'Expression on left side of ?? is not nullable.',
73,
Expand Down Expand Up @@ -377,4 +365,10 @@ public function testBug13921(): void
]);
}

#[RequiresPhp('>= 8.0')]
public function testBug14311(): void
{
$this->analyse([__DIR__ . '/data/bug-14311.php'], []);
}

}
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14311.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug14311;

class Station {
public string $address = '';
}

/** @param array<string, Station> $stations */
function withNullCoalesce(array $stations, string $key): string {
$bar = $stations[$key] ?? null;
return $bar?->address ?? 'Unknown';
}

/** @param array<string, Station> $stations */
function withIsset(array $stations, string $key): string {
$bar = isset($stations[$key]) ? $stations[$key] : null;
return $bar?->address ?? 'Unknown';
}

/** @param array<string, Station> $stations */
function withArrayKeyExists(array $stations, string $key): string {
$bar = array_key_exists($key, $stations) ? $stations[$key] : null;
return $bar?->address ?? 'Unknown';
}
Loading