Skip to content

Commit 0094c4f

Browse files
authored
[TASK] Add PHPStan rules from Rector Type Perfect (#939)
This is an opiniated set of PHPStan rules to make code more explicit and intuitive to read. https://github.com/rectorphp/type-perfect The new rules show some parts where we could improve our code, which have now been added to the PHPStan baseline.
1 parent 1acd24d commit 0094c4f

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"phpstan/phpstan-phpunit": "1.4.2 || 2.0.4",
3434
"phpstan/phpstan-strict-rules": "1.6.2 || 2.0.3",
3535
"phpunit/phpunit": "8.5.41",
36-
"rector/rector": "1.2.10 || 2.0.7"
36+
"rector/rector": "1.2.10 || 2.0.7",
37+
"rector/type-perfect": "1.0.0 || 2.0.2"
3738
},
3839
"suggest": {
3940
"ext-mbstring": "for parsing UTF-8 CSS"

config/phpstan-baseline.neon

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ parameters:
7878
count: 1
7979
path: ../src/CSSList/Document.php
8080

81+
-
82+
message: '#^Parameters should have "string\|null" types as the only types passed to this method$#'
83+
identifier: typePerfect.narrowPublicClassMethodParamType
84+
count: 1
85+
path: ../src/CSSList/Document.php
86+
8187
-
8288
message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#'
8389
identifier: method.notFound
@@ -90,6 +96,24 @@ parameters:
9096
count: 1
9197
path: ../src/CSSList/KeyFrame.php
9298

99+
-
100+
message: '#^Parameters should have "string" types as the only types passed to this method$#'
101+
identifier: typePerfect.narrowPublicClassMethodParamType
102+
count: 2
103+
path: ../src/CSSList/KeyFrame.php
104+
105+
-
106+
message: '#^Parameters should have "string\|string" types as the only types passed to this method$#'
107+
identifier: typePerfect.narrowPublicClassMethodParamType
108+
count: 1
109+
path: ../src/OutputFormat.php
110+
111+
-
112+
message: '#^Returning false in non return bool class method\. Use null with type\|null instead or add bool return type$#'
113+
identifier: typePerfect.nullOverFalse
114+
count: 1
115+
path: ../src/OutputFormat.php
116+
93117
-
94118
message: '#^Variable property access on \$this\(Sabberworm\\CSS\\OutputFormat\)\.$#'
95119
identifier: property.dynamicName
@@ -132,6 +156,30 @@ parameters:
132156
count: 1
133157
path: ../src/Parsing/ParserState.php
134158

159+
-
160+
message: '#^Parameters should have "bool" types as the only types passed to this method$#'
161+
identifier: typePerfect.narrowPublicClassMethodParamType
162+
count: 2
163+
path: ../src/Parsing/ParserState.php
164+
165+
-
166+
message: '#^Parameters should have "int" types as the only types passed to this method$#'
167+
identifier: typePerfect.narrowPublicClassMethodParamType
168+
count: 2
169+
path: ../src/Parsing/ParserState.php
170+
171+
-
172+
message: '#^Parameters should have "string" types as the only types passed to this method$#'
173+
identifier: typePerfect.narrowPublicClassMethodParamType
174+
count: 1
175+
path: ../src/Parsing/ParserState.php
176+
177+
-
178+
message: '#^Parameters should have "string\|int\|null" types as the only types passed to this method$#'
179+
identifier: typePerfect.narrowPublicClassMethodParamType
180+
count: 1
181+
path: ../src/Parsing/ParserState.php
182+
135183
-
136184
message: '#^Cannot call method render\(\) on string\.$#'
137185
identifier: method.nonObject
@@ -180,6 +228,12 @@ parameters:
180228
count: 1
181229
path: ../src/Property/Import.php
182230

231+
-
232+
message: '#^Parameters should have "string" types as the only types passed to this method$#'
233+
identifier: typePerfect.narrowPublicClassMethodParamType
234+
count: 1
235+
path: ../src/Property/Selector.php
236+
183237
-
184238
message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#'
185239
identifier: method.notFound
@@ -204,6 +258,24 @@ parameters:
204258
count: 1
205259
path: ../src/Rule/Rule.php
206260

261+
-
262+
message: '#^Parameters should have "bool" types as the only types passed to this method$#'
263+
identifier: typePerfect.narrowPublicClassMethodParamType
264+
count: 1
265+
path: ../src/Rule/Rule.php
266+
267+
-
268+
message: '#^Parameters should have "int\|int" types as the only types passed to this method$#'
269+
identifier: typePerfect.narrowPublicClassMethodParamType
270+
count: 1
271+
path: ../src/Rule/Rule.php
272+
273+
-
274+
message: '#^Parameters should have "string" types as the only types passed to this method$#'
275+
identifier: typePerfect.narrowPublicClassMethodParamType
276+
count: 1
277+
path: ../src/Rule/Rule.php
278+
207279
-
208280
message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:comments\(\)\.$#'
209281
identifier: method.notFound
@@ -270,6 +342,18 @@ parameters:
270342
count: 1
271343
path: ../src/RuleSet/DeclarationBlock.php
272344

345+
-
346+
message: '#^Parameters should have "string" types as the only types passed to this method$#'
347+
identifier: typePerfect.narrowPublicClassMethodParamType
348+
count: 1
349+
path: ../src/RuleSet/DeclarationBlock.php
350+
351+
-
352+
message: '#^Returning false in non return bool class method\. Use null with type\|null instead or add bool return type$#'
353+
identifier: typePerfect.nullOverFalse
354+
count: 1
355+
path: ../src/RuleSet/DeclarationBlock.php
356+
273357
-
274358
message: '#^Argument of an invalid type Sabberworm\\CSS\\Rule\\Rule supplied for foreach, only iterables are supported\.$#'
275359
identifier: foreach.nonIterable
@@ -324,6 +408,24 @@ parameters:
324408
count: 1
325409
path: ../src/RuleSet/RuleSet.php
326410

411+
-
412+
message: '#^Parameters should have "Sabberworm\\CSS\\Rule\\Rule" types as the only types passed to this method$#'
413+
identifier: typePerfect.narrowPublicClassMethodParamType
414+
count: 1
415+
path: ../src/RuleSet/RuleSet.php
416+
417+
-
418+
message: '#^Parameters should have "string" types as the only types passed to this method$#'
419+
identifier: typePerfect.narrowPublicClassMethodParamType
420+
count: 1
421+
path: ../src/RuleSet/RuleSet.php
422+
423+
-
424+
message: '#^Use explicit methods over array access on object$#'
425+
identifier: typePerfect.noArrayAccessOnObject
426+
count: 1
427+
path: ../src/RuleSet/RuleSet.php
428+
327429
-
328430
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
329431
identifier: notEqual.notAllowed
@@ -372,12 +474,24 @@ parameters:
372474
count: 3
373475
path: ../src/Value/Color.php
374476

477+
-
478+
message: '#^Provide more specific return type "Sabberworm\\CSS\\Value\\Color" over abstract one$#'
479+
identifier: typePerfect.narrowReturnObjectType
480+
count: 1
481+
path: ../src/Value/Color.php
482+
375483
-
376484
message: '#^Loose comparison via "\!\=" is not allowed\.$#'
377485
identifier: notEqual.notAllowed
378486
count: 1
379487
path: ../src/Value/Size.php
380488

489+
-
490+
message: '#^Parameters should have "float" types as the only types passed to this method$#'
491+
identifier: typePerfect.narrowPublicClassMethodParamType
492+
count: 1
493+
path: ../src/Value/Size.php
494+
381495
-
382496
message: '#^Call to an undefined method Sabberworm\\CSS\\OutputFormat\:\:implode\(\)\.$#'
383497
identifier: method.notFound

config/phpstan.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ parameters:
1212
- %currentWorkingDirectory%/bin/
1313
- %currentWorkingDirectory%/src/
1414
- %currentWorkingDirectory%/tests/
15+
16+
type_perfect:
17+
no_mixed_property: true
18+
no_mixed_caller: true
19+
null_over_false: true
20+
narrow_param: true
21+
narrow_return: true

0 commit comments

Comments
 (0)