|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ImpureConstuctor; |
| 4 | + |
| 5 | +use function PHPStan\Testing\assertType; |
| 6 | + |
| 7 | +class Foo |
| 8 | +{ |
| 9 | + |
| 10 | + /** @var bool */ |
| 11 | + private $active; |
| 12 | + |
| 13 | + public function __construct() |
| 14 | + { |
| 15 | + $this->active = false; |
| 16 | + } |
| 17 | + |
| 18 | + public function getActive(): bool |
| 19 | + { |
| 20 | + return $this->active; |
| 21 | + } |
| 22 | + |
| 23 | + public function activate(): void |
| 24 | + { |
| 25 | + $this->active = true; |
| 26 | + } |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +class ClassWithImpureConstructorNotMarked |
| 32 | +{ |
| 33 | + |
| 34 | + public function __construct(Foo $foo) |
| 35 | + { |
| 36 | + $foo->activate(); |
| 37 | + } |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +class ClassWithImpureConstructorMarked |
| 42 | +{ |
| 43 | + |
| 44 | + /** |
| 45 | + * @phpstan-impure |
| 46 | + */ |
| 47 | + public function __construct(Foo $foo) |
| 48 | + { |
| 49 | + $foo->activate(); |
| 50 | + } |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +class ClassWithPureConstructorMarked |
| 55 | +{ |
| 56 | + |
| 57 | + /** |
| 58 | + * @phpstan-pure |
| 59 | + */ |
| 60 | + public function __construct(Foo $foo) |
| 61 | + { |
| 62 | + } |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +class ClassWithImpureConstructorNotMarkedWithoutParameters |
| 67 | +{ |
| 68 | + |
| 69 | + /** @var string */ |
| 70 | + private $lorem; |
| 71 | + |
| 72 | + public function __construct() |
| 73 | + { |
| 74 | + $this->lorem = 'lorem'; |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +class Test |
| 80 | +{ |
| 81 | + |
| 82 | + public function testClassWithImpureConstructorNotMarked() |
| 83 | + { |
| 84 | + $foo = new Foo(); |
| 85 | + assertType('bool', $foo->getActive()); |
| 86 | + |
| 87 | + assert(!$foo->getActive()); |
| 88 | + assertType('false', $foo->getActive()); |
| 89 | + |
| 90 | + new ClassWithImpureConstructorNotMarked($foo); |
| 91 | + assertType('false', $foo->getActive()); |
| 92 | + } |
| 93 | + |
| 94 | + public function testClassWithImpureConstructorMarked() |
| 95 | + { |
| 96 | + $foo = new Foo(); |
| 97 | + assertType('bool', $foo->getActive()); |
| 98 | + |
| 99 | + assert(!$foo->getActive()); |
| 100 | + assertType('false', $foo->getActive()); |
| 101 | + |
| 102 | + new ClassWithImpureConstructorMarked($foo); |
| 103 | + assertType('bool', $foo->getActive()); |
| 104 | + } |
| 105 | + |
| 106 | + public function testClassWithPureConstructorMarked() |
| 107 | + { |
| 108 | + $foo = new Foo(); |
| 109 | + assertType('bool', $foo->getActive()); |
| 110 | + |
| 111 | + assert(!$foo->getActive()); |
| 112 | + assertType('false', $foo->getActive()); |
| 113 | + |
| 114 | + new ClassWithPureConstructorMarked($foo); |
| 115 | + assertType('false', $foo->getActive()); |
| 116 | + } |
| 117 | + |
| 118 | + public function testClassWithImpureConstructorNotMarkedWithoutParameters() |
| 119 | + { |
| 120 | + $foo = new Foo(); |
| 121 | + assertType('bool', $foo->getActive()); |
| 122 | + |
| 123 | + assert(!$foo->getActive()); |
| 124 | + assertType('false', $foo->getActive()); |
| 125 | + |
| 126 | + new ClassWithImpureConstructorNotMarkedWithoutParameters(); |
| 127 | + assertType('false', $foo->getActive()); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments