Skip to content

Commit 3f7c015

Browse files
committed
Refactoring
1 parent ec615d7 commit 3f7c015

File tree

4 files changed

+112
-51
lines changed

4 files changed

+112
-51
lines changed

conf/config.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,11 @@ services:
674674
-
675675
class: PHPStan\Rules\AttributesCheck
676676

677+
-
678+
class: PHPStan\Rules\Arrays\NonexistentOffsetInArrayDimFetchCheck
679+
arguments:
680+
reportMaybes: %reportMaybes%
681+
677682
-
678683
class: PHPStan\Rules\ClassCaseSensitivityCheck
679684
arguments:
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Arrays;
4+
5+
use PhpParser\Node\Expr;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Rules\RuleError;
8+
use PHPStan\Rules\RuleErrorBuilder;
9+
use PHPStan\Rules\RuleLevelHelper;
10+
use PHPStan\Type\ErrorType;
11+
use PHPStan\Type\Type;
12+
use PHPStan\Type\TypeUtils;
13+
use PHPStan\Type\UnionType;
14+
use PHPStan\Type\VerbosityLevel;
15+
16+
class NonexistentOffsetInArrayDimFetchCheck
17+
{
18+
19+
private RuleLevelHelper $ruleLevelHelper;
20+
21+
private bool $reportMaybes;
22+
23+
public function __construct(RuleLevelHelper $ruleLevelHelper, bool $reportMaybes)
24+
{
25+
$this->ruleLevelHelper = $ruleLevelHelper;
26+
$this->reportMaybes = $reportMaybes;
27+
}
28+
29+
/**
30+
* @param Scope $scope
31+
* @param Expr $var
32+
* @param string $unknownClassPattern
33+
* @param Type $dimType
34+
* @return RuleError[]
35+
*/
36+
public function check(
37+
Scope $scope,
38+
Expr $var,
39+
string $unknownClassPattern,
40+
Type $dimType
41+
): array
42+
{
43+
$typeResult = $this->ruleLevelHelper->findTypeToCheck(
44+
$scope,
45+
$var,
46+
$unknownClassPattern,
47+
static function (Type $type) use ($dimType): bool {
48+
return $type->hasOffsetValueType($dimType)->yes();
49+
}
50+
);
51+
$type = $typeResult->getType();
52+
if ($type instanceof ErrorType) {
53+
return $typeResult->getUnknownClassErrors();
54+
}
55+
56+
$hasOffsetValueType = $type->hasOffsetValueType($dimType);
57+
$report = $hasOffsetValueType->no();
58+
if ($hasOffsetValueType->maybe()) {
59+
$constantArrays = TypeUtils::getOldConstantArrays($type);
60+
if (count($constantArrays) > 0) {
61+
foreach ($constantArrays as $constantArray) {
62+
if ($constantArray->hasOffsetValueType($dimType)->no()) {
63+
$report = true;
64+
break;
65+
}
66+
}
67+
}
68+
}
69+
70+
if (!$report && $this->reportMaybes) {
71+
foreach (TypeUtils::flattenTypes($type) as $innerType) {
72+
if ($dimType instanceof UnionType) {
73+
if ($innerType->hasOffsetValueType($dimType)->no()) {
74+
$report = true;
75+
break;
76+
}
77+
continue;
78+
}
79+
foreach (TypeUtils::flattenTypes($dimType) as $innerDimType) {
80+
if ($innerType->hasOffsetValueType($innerDimType)->no()) {
81+
$report = true;
82+
break;
83+
}
84+
}
85+
}
86+
}
87+
88+
if ($report) {
89+
return [
90+
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))->build(),
91+
];
92+
}
93+
94+
return [];
95+
}
96+
97+
}

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use PHPStan\Rules\RuleLevelHelper;
88
use PHPStan\Type\ErrorType;
99
use PHPStan\Type\Type;
10-
use PHPStan\Type\TypeUtils;
11-
use PHPStan\Type\UnionType;
1210
use PHPStan\Type\VerbosityLevel;
1311

1412
/**
@@ -19,14 +17,18 @@ class NonexistentOffsetInArrayDimFetchRule implements \PHPStan\Rules\Rule
1917

2018
private RuleLevelHelper $ruleLevelHelper;
2119

20+
private NonexistentOffsetInArrayDimFetchCheck $nonexistentOffsetInArrayDimFetchCheck;
21+
2222
private bool $reportMaybes;
2323

2424
public function __construct(
2525
RuleLevelHelper $ruleLevelHelper,
26+
NonexistentOffsetInArrayDimFetchCheck $nonexistentOffsetInArrayDimFetchCheck,
2627
bool $reportMaybes
2728
)
2829
{
2930
$this->ruleLevelHelper = $ruleLevelHelper;
31+
$this->nonexistentOffsetInArrayDimFetchCheck = $nonexistentOffsetInArrayDimFetchCheck;
3032
$this->reportMaybes = $reportMaybes;
3133
}
3234

@@ -91,58 +93,12 @@ static function (Type $type): bool {
9193
return [];
9294
}
9395

94-
$typeResult = $this->ruleLevelHelper->findTypeToCheck(
96+
return $this->nonexistentOffsetInArrayDimFetchCheck->check(
9597
$scope,
9698
$node->var,
9799
$unknownClassPattern,
98-
static function (Type $type) use ($dimType): bool {
99-
return $type->hasOffsetValueType($dimType)->yes();
100-
}
100+
$dimType
101101
);
102-
$type = $typeResult->getType();
103-
if ($type instanceof ErrorType) {
104-
return $typeResult->getUnknownClassErrors();
105-
}
106-
107-
$hasOffsetValueType = $type->hasOffsetValueType($dimType);
108-
$report = $hasOffsetValueType->no();
109-
if ($hasOffsetValueType->maybe()) {
110-
$constantArrays = TypeUtils::getOldConstantArrays($type);
111-
if (count($constantArrays) > 0) {
112-
foreach ($constantArrays as $constantArray) {
113-
if ($constantArray->hasOffsetValueType($dimType)->no()) {
114-
$report = true;
115-
break;
116-
}
117-
}
118-
}
119-
}
120-
121-
if (!$report && $this->reportMaybes) {
122-
foreach (TypeUtils::flattenTypes($type) as $innerType) {
123-
if ($dimType instanceof UnionType) {
124-
if ($innerType->hasOffsetValueType($dimType)->no()) {
125-
$report = true;
126-
break;
127-
}
128-
continue;
129-
}
130-
foreach (TypeUtils::flattenTypes($dimType) as $innerDimType) {
131-
if ($innerType->hasOffsetValueType($innerDimType)->no()) {
132-
$report = true;
133-
break;
134-
}
135-
}
136-
}
137-
}
138-
139-
if ($report) {
140-
return [
141-
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))->build(),
142-
];
143-
}
144-
145-
return [];
146102
}
147103

148104
}

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ class NonexistentOffsetInArrayDimFetchRuleTest extends \PHPStan\Testing\RuleTest
1212

1313
protected function getRule(): \PHPStan\Rules\Rule
1414
{
15+
$ruleLevelHelper = new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false);
16+
1517
return new NonexistentOffsetInArrayDimFetchRule(
16-
new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false),
18+
$ruleLevelHelper,
19+
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true),
1720
true
1821
);
1922
}

0 commit comments

Comments
 (0)