File tree Expand file tree Collapse file tree 10 files changed +304
-6
lines changed
src/Rules/TooWideTypehints
tests/PHPStan/Rules/TooWideTypehints Expand file tree Collapse file tree 10 files changed +304
-6
lines changed Original file line number Diff line number Diff line change @@ -98,6 +98,13 @@ public function checkFunction(
9898
9999 $ returnType = TypeCombinator::union (...$ returnTypes );
100100
101+ if (
102+ $ returnType ->isConstantScalarValue ()->yes ()
103+ && $ functionReturnType ->isConstantScalarValue ()->yes ()
104+ ) {
105+ return [];
106+ }
107+
101108 // Do not require to have @return null/true/false in descendant classes
102109 if (
103110 $ checkDescendantClass
Original file line number Diff line number Diff line change @@ -18,6 +18,11 @@ protected function getRule(): Rule
1818 );
1919 }
2020
21+ public function testBug10312e (): void
22+ {
23+ $ this ->analyse ([__DIR__ . '/data/bug-10312e.php ' ], []);
24+ }
25+
2126 public function testRule (): void
2227 {
2328 $ this ->analyse ([__DIR__ . '/data/tooWideClosureReturnType.php ' ], [
Original file line number Diff line number Diff line change @@ -61,4 +61,9 @@ public function testBug11980(): void
6161 ]);
6262 }
6363
64+ public function testBug10312a (): void
65+ {
66+ $ this ->analyse ([__DIR__ . '/data/bug-10312a.php ' ], []);
67+ }
68+
6469}
Original file line number Diff line number Diff line change @@ -88,12 +88,7 @@ public function testPublicProtectedWithInheritance(): void
8888
8989 public function testBug5095 (): void
9090 {
91- $ this ->analyse ([__DIR__ . '/data/bug-5095.php ' ], [
92- [
93- 'Method Bug5095\Parser::unaryOperatorFor() never returns \'not \' so it can be removed from the return type. ' ,
94- 21 ,
95- ],
96- ]);
91+ $ this ->analyse ([__DIR__ . '/data/bug-5095.php ' ], []);
9792 }
9893
9994 #[RequiresPhp('>= 8.0 ' )]
@@ -199,4 +194,28 @@ public function testBug11980(): void
199194 ]);
200195 }
201196
197+ public function testBug10312 (): void
198+ {
199+ $ this ->checkProtectedAndPublicMethods = true ;
200+ $ this ->analyse ([__DIR__ . '/data/bug-10312.php ' ], []);
201+ }
202+
203+ public function testBug10312b (): void
204+ {
205+ $ this ->checkProtectedAndPublicMethods = true ;
206+ $ this ->analyse ([__DIR__ . '/data/bug-10312b.php ' ], []);
207+ }
208+
209+ public function testBug10312c (): void
210+ {
211+ $ this ->checkProtectedAndPublicMethods = true ;
212+ $ this ->analyse ([__DIR__ . '/data/bug-10312c.php ' ], []);
213+ }
214+
215+ public function testBug10312d (): void
216+ {
217+ $ this ->checkProtectedAndPublicMethods = true ;
218+ $ this ->analyse ([__DIR__ . '/data/bug-10312d.php ' ], []);
219+ }
220+
202221}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Bug10312 ;
4+
5+ class Cmsissue
6+ {
7+ public const LANE_BACKLOG = 'Backlog ' ;
8+
9+ public const LANE_READY_FOR_IMPL = 'Bereit zur Entwicklung ' ;
10+
11+ public const LANE_IN_IMPL = 'In Entwicklung ' ;
12+
13+ public const LANE_READY_FOR_UAT = 'Bereit zur Abnahme ' ;
14+
15+ public const LANE_READY_FOR_UAT_KANBAN = 'Test ' ;
16+
17+ public const LANE_PROJECT_BACKLOG = 'Backlog ' ;
18+
19+ public const LANE_PROJECT_IN_IMPL = 'Projekt in Umsetzung ' ;
20+
21+ public const LANE_SKIP = '' ;
22+ }
23+
24+
25+ final class Mapper
26+ {
27+
28+ /**
29+ * @return Cmsissue::LANE_*
30+ */
31+ public function mapIssueStatus (): string
32+ {
33+ if (rand (0 ,1 ) === 0 ) {
34+ return Cmsissue::LANE_BACKLOG ;
35+ }
36+ if (rand (0 ,1 ) === 0 ) {
37+ return Cmsissue::LANE_READY_FOR_IMPL ;
38+ }
39+ if (rand (0 ,1 ) === 0 ) {
40+ return Cmsissue::LANE_IN_IMPL ;
41+ }
42+ if (rand (0 ,1 ) === 0 ) {
43+ return Cmsissue::LANE_READY_FOR_UAT ;
44+ }
45+
46+ return Cmsissue::LANE_SKIP ;
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Bug10312a ;
4+
5+ class Cmsissue
6+ {
7+ public const LANE_BACKLOG = 'Backlog ' ;
8+
9+ public const LANE_READY_FOR_IMPL = 'Bereit zur Entwicklung ' ;
10+
11+ public const LANE_IN_IMPL = 'In Entwicklung ' ;
12+
13+ public const LANE_READY_FOR_UAT = 'Bereit zur Abnahme ' ;
14+
15+ public const LANE_READY_FOR_UAT_KANBAN = 'Test ' ;
16+
17+ public const LANE_PROJECT_BACKLOG = 'Backlog ' ;
18+
19+ public const LANE_PROJECT_IN_IMPL = 'Projekt in Umsetzung ' ;
20+
21+ public const LANE_SKIP = '' ;
22+ }
23+
24+
25+ /**
26+ * @return Cmsissue::LANE_*
27+ */
28+ function mapIssueStatus (): string
29+ {
30+ if (rand (0 ,1 ) === 0 ) {
31+ return Cmsissue::LANE_BACKLOG ;
32+ }
33+ if (rand (0 ,1 ) === 0 ) {
34+ return Cmsissue::LANE_READY_FOR_IMPL ;
35+ }
36+ if (rand (0 ,1 ) === 0 ) {
37+ return Cmsissue::LANE_IN_IMPL ;
38+ }
39+ if (rand (0 ,1 ) === 0 ) {
40+ return Cmsissue::LANE_READY_FOR_UAT ;
41+ }
42+
43+ return Cmsissue::LANE_SKIP ;
44+ }
45+
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Bug10312b ;
4+
5+ interface MinPhpVersionInterface
6+ {
7+ /**
8+ * @return PhpVersion::*
9+ */
10+ public function provideMinPhpVersion () : int ;
11+ }
12+
13+ final class PhpVersion
14+ {
15+ /**
16+ * @var int
17+ */
18+ public const PHP_52 = 50200 ;
19+ /**
20+ * @var int
21+ */
22+ public const PHP_53 = 50300 ;
23+ /**
24+ * @var int
25+ */
26+ public const PHP_54 = 50400 ;
27+ /**
28+ * @var int
29+ */
30+ public const PHP_55 = 50500 ;
31+ }
32+
33+ final class TypedPropertyFromStrictConstructorReadonlyClassRector implements MinPhpVersionInterface
34+ {
35+ public function provideMinPhpVersion (): int
36+ {
37+ return PhpVersion::PHP_55 ;
38+ }
39+
40+ }
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.1
2+
3+ declare (strict_types = 1 );
4+
5+ namespace Bug10312c ;
6+
7+ enum Foo: int
8+ {
9+ case BAR = 1 ;
10+ case BAZ = 2 ;
11+ }
12+
13+ interface ReturnsFoo
14+ {
15+ /** @return value-of<Foo> */
16+ public function returnsFooValue (): int ;
17+ }
18+
19+ class ReturnsBar implements ReturnsFoo
20+ {
21+ #[\Override]
22+ public function returnsFooValue (): int
23+ {
24+ return Foo::BAR ->value ;
25+ }
26+ }
27+
28+ class ReturnsBarWithFinalMethod implements ReturnsFoo
29+ {
30+ #[\Override]
31+ final public function returnsFooValue (): int
32+ {
33+ return Foo::BAR ->value ;
34+ }
35+ }
36+
37+ final class ReturnsBaz implements ReturnsFoo
38+ {
39+ #[\Override]
40+ public function returnsFooValue (): int
41+ {
42+ return Foo::BAZ ->value ;
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.1
2+
3+ declare (strict_types = 1 );
4+
5+ namespace Bug10312d ;
6+
7+ enum Foo: int
8+ {
9+ case BAR = 1 ;
10+ case BAZ = 2 ;
11+ }
12+
13+ class FooBar {
14+ public ?Foo $ foo = null ;
15+ }
16+
17+ interface ReturnsFoo
18+ {
19+ /** @return value-of<Foo> */
20+ public function returnsFooValue (): int ;
21+
22+ /** @return value-of<Foo>|null */
23+ public function returnsFooOrNullValue (): ?int ;
24+ }
25+
26+ final class ReturnsNullsafeBaz implements ReturnsFoo
27+ {
28+ #[\Override]
29+ public function returnsFooValue (): int
30+ {
31+ $ f = new FooBar ();
32+ return $ f ->foo ?->value;
33+ }
34+
35+ #[\Override]
36+ public function returnsFooOrNullValue (): ?int
37+ {
38+ $ f = new FooBar ();
39+ return $ f ->foo ?->value;
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Bug10312e ;
4+
5+ class Cmsissue
6+ {
7+ public const LANE_BACKLOG = 'Backlog ' ;
8+
9+ public const LANE_READY_FOR_IMPL = 'Bereit zur Entwicklung ' ;
10+
11+ public const LANE_IN_IMPL = 'In Entwicklung ' ;
12+
13+ public const LANE_READY_FOR_UAT = 'Bereit zur Abnahme ' ;
14+
15+ public const LANE_READY_FOR_UAT_KANBAN = 'Test ' ;
16+
17+ public const LANE_PROJECT_BACKLOG = 'Backlog ' ;
18+
19+ public const LANE_PROJECT_IN_IMPL = 'Projekt in Umsetzung ' ;
20+
21+ public const LANE_SKIP = '' ;
22+ }
23+
24+
25+ /**
26+ * @return Cmsissue::LANE_*
27+ */
28+ $ x = function (): string
29+ {
30+ if (rand (0 ,1 ) === 0 ) {
31+ return Cmsissue::LANE_BACKLOG ;
32+ }
33+ if (rand (0 ,1 ) === 0 ) {
34+ return Cmsissue::LANE_READY_FOR_IMPL ;
35+ }
36+ if (rand (0 ,1 ) === 0 ) {
37+ return Cmsissue::LANE_IN_IMPL ;
38+ }
39+ if (rand (0 ,1 ) === 0 ) {
40+ return Cmsissue::LANE_READY_FOR_UAT ;
41+ }
42+
43+ return Cmsissue::LANE_SKIP ;
44+ };
You can’t perform that action at this time.
0 commit comments