forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisset-method-called-from-constructor.php
More file actions
77 lines (63 loc) · 1.54 KB
/
isset-method-called-from-constructor.php
File metadata and controls
77 lines (63 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types = 1);
namespace IssetMethodCalledFromConstructor;
final class MethodCalledFromConstructor {
private int $bar;
public function __construct(int $bar)
{
$this->setBar($bar);
}
private function setBar(int $bar): void
{
if (isset($this->bar)) { // $bar has no default, could be uninitialized when called from constructor - no error
throw new \Exception('bar is set');
}
$this->bar = $bar;
}
}
final class MethodCalledFromConstructorWithDefault {
private int $bar = 1;
public function __construct(int $bar)
{
$this->setBar($bar);
}
private function setBar(int $bar): void
{
if (isset($this->bar)) { // $bar has default value, always initialized - should error
throw new \Exception('bar is set');
}
$this->bar = $bar;
}
}
final class MethodNotCalledFromConstructor {
private int $bar;
public function __construct(int $bar)
{
$this->bar = $bar;
}
private function checkBar(): void
{
if (isset($this->bar)) { // Not called from constructor, property is initialized after construction - should error
echo 'bar is set';
}
}
}
final class MultipleProperties {
private int $foo;
private int $bar = 5;
public function __construct(int $bar)
{
$this->init($bar);
$this->foo = 42;
}
private function init(int $bar): void
{
if (isset($this->foo)) { // $foo has no default, could be uninitialized - no error
throw new \Exception('foo is set');
}
if (isset($this->bar)) { // $bar has default value, always initialized - should error
echo 'bar is set';
}
$this->bar = $bar;
}
}