88use PhpParser \NodeVisitorAbstract ;
99use PHPStan \Php \PhpMinorVersionIterator ;
1010use PHPStan \Php \PhpVersion ;
11+ use PHPStan \ShouldNotHappenException ;
1112use function in_array ;
1213use function strtolower ;
1314
1920 */
2021final class RequiredPhpVersionVisitor extends NodeVisitorAbstract
2122{
23+
2224 private ?int $ requiredVersionId = null ;
2325
2426 private ?string $ reason = null ;
@@ -44,64 +46,64 @@ public function getReasonLine(): ?int
4446 public function enterNode (Node $ node ): ?Node
4547 {
4648 if ($ node instanceof Node \Stmt \Enum_) {
47- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsEnums (), 'enums ' , $ node );
49+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsEnums (), 'enums ' , $ node );
4850 }
4951
5052 if ($ node instanceof Node \Expr \BinaryOp \Pipe) {
51- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsPipeOperator (), 'the pipe operator ' , $ node );
53+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsPipeOperator (), 'the pipe operator ' , $ node );
5254 }
5355
5456 if ($ node instanceof Node \PropertyHook) {
55- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsPropertyHooks (), 'property hooks ' , $ node );
57+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsPropertyHooks (), 'property hooks ' , $ node );
5658 }
5759
5860 if ($ node instanceof Node \IntersectionType) {
59- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsPureIntersectionTypes (), 'pure intersection types ' , $ node );
61+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsPureIntersectionTypes (), 'pure intersection types ' , $ node );
6062 }
6163
6264 if ($ node instanceof Node \UnionType) {
63- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsNativeUnionTypes (), 'union types ' , $ node );
65+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsNativeUnionTypes (), 'union types ' , $ node );
6466 foreach ($ node ->types as $ innerType ) {
6567 if ($ innerType instanceof Node \IntersectionType) {
66- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsDisjunctiveNormalForm (), 'disjunctive normal form types ' , $ innerType );
68+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsDisjunctiveNormalForm (), 'disjunctive normal form types ' , $ innerType );
6769 }
6870 if (!($ innerType instanceof Node \Identifier) || strtolower ($ innerType ->name ) !== 'true ' ) {
6971 continue ;
7072 }
7173
72- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsTrueFalseNullStandaloneType (), 'the standalone "true" type ' , $ innerType );
74+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsTrueFalseNullStandaloneType (), 'the standalone "true" type ' , $ innerType );
7375 }
7476 }
7577
7678 if ($ node instanceof Node \Stmt \Class_ && ($ node ->flags & Modifiers::READONLY ) !== 0 ) {
77- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsReadOnlyClasses (), 'readonly classes ' , $ node );
79+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsReadOnlyClasses (), 'readonly classes ' , $ node );
7880 }
7981
8082 if ($ node instanceof Node \Stmt \Property && ($ node ->flags & Modifiers::READONLY ) !== 0 ) {
81- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsReadOnlyProperties (), 'readonly properties ' , $ node );
83+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsReadOnlyProperties (), 'readonly properties ' , $ node );
8284 }
8385
8486 if ($ node instanceof Node \Param && $ node ->flags !== 0 ) {
85- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsPromotedProperties (), 'promoted properties ' , $ node );
87+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsPromotedProperties (), 'promoted properties ' , $ node );
8688 }
8789
8890 if ($ node instanceof Node \Param && ($ node ->flags & Modifiers::READONLY ) !== 0 ) {
89- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsReadOnlyProperties (), 'readonly promoted properties ' , $ node );
91+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsReadOnlyProperties (), 'readonly promoted properties ' , $ node );
9092 }
9193
9294 if (
9395 ($ node instanceof Node \Param || $ node instanceof Node \Stmt \Property)
9496 && ($ node ->flags & Modifiers::VISIBILITY_SET_MASK ) !== 0
9597 ) {
96- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsAsymmetricVisibility (), 'asymmetric visibility ' , $ node );
98+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsAsymmetricVisibility (), 'asymmetric visibility ' , $ node );
9799 }
98100
99101 if ($ node instanceof Node \Stmt \ClassConst && $ node ->type !== null ) {
100- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsNativeTypesInClassConstants (), 'typed class constants ' , $ node );
102+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsNativeTypesInClassConstants (), 'typed class constants ' , $ node );
101103 }
102104
103105 if ($ node instanceof Node \Expr \ClassConstFetch && $ node ->name instanceof Node \Expr) {
104- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsDynamicClassConstantFetch (), 'dynamic class constant fetch ' , $ node );
106+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsDynamicClassConstantFetch (), 'dynamic class constant fetch ' , $ node );
105107 }
106108
107109 if (
@@ -112,14 +114,14 @@ public function enterNode(Node $node): ?Node
112114 ) {
113115 foreach ($ node ->args as $ arg ) {
114116 if ($ arg instanceof Node \VariadicPlaceholder) {
115- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsFirstClassCallables (), 'first-class callable syntax ' , $ arg );
117+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsFirstClassCallables (), 'first-class callable syntax ' , $ arg );
116118 break ;
117119 }
118120 }
119121 }
120122
121123 if ($ node instanceof Node \Arg && $ node ->name !== null ) {
122- $ this ->require (fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsNamedArguments (), 'named arguments ' , $ node );
124+ $ this ->require (static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsNamedArguments (), 'named arguments ' , $ node );
123125 }
124126
125127 $ this ->checkStandaloneType ($ node );
@@ -140,9 +142,9 @@ private function checkStandaloneType(Node $node): void
140142 }
141143
142144 $ this ->require (
143- fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsTrueFalseNullStandaloneType (),
145+ static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsTrueFalseNullStandaloneType (),
144146 'standalone "null", "false" or "true" types ' ,
145- $ type
147+ $ type,
146148 );
147149 }
148150
@@ -158,9 +160,9 @@ private function checkMixedType(Node $node): void
158160 }
159161
160162 $ this ->require (
161- fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsNativeMixed (),
163+ static fn (PhpVersion $ phpVersion ) => $ phpVersion ->supportsNativeMixed (),
162164 'the mixed type ' ,
163- $ type
165+ $ type,
164166 );
165167 }
166168
@@ -209,16 +211,16 @@ private function findPhpVersion(callable $callable): int
209211 {
210212 $ minorVersionIterator = new PhpMinorVersionIterator (
211213 new PhpVersion (70400 ),
212- new PhpVersion (80500 )
214+ new PhpVersion (80500 ),
213215 );
214216
215- foreach ($ minorVersionIterator as $ phpVersion ) {
217+ foreach ($ minorVersionIterator as $ phpVersion ) {
216218 if ($ callable ($ phpVersion )) {
217219 return $ phpVersion ->getVersionId ();
218220 }
219221 }
220222
221- throw new \ PHPStan \ ShouldNotHappenException ();
223+ throw new ShouldNotHappenException ();
222224 }
223225
224226}
0 commit comments