1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PropertiesFromArrayIntoObject ;
4
+
5
+ class Foo
6
+ {
7
+ /**
8
+ * @var string
9
+ */
10
+ public $ foo = '' ;
11
+
12
+ /**
13
+ * @var float
14
+ */
15
+ public $ float_test = 0.0 ;
16
+
17
+ /**
18
+ * @var int
19
+ */
20
+ public $ lall = 0 ;
21
+
22
+ /**
23
+ * @var int|null
24
+ */
25
+ public $ test ;
26
+
27
+ /**
28
+ * @phpstan-return array{float_test: float, foo: 'bar', lall: string, noop: int, test: int}
29
+ */
30
+ public function data (): array
31
+ {
32
+ /** @var mixed $array */
33
+ $ array = [];
34
+
35
+ return $ array ;
36
+ }
37
+
38
+ public function create_simple_0 (): self {
39
+ $ self = new self ();
40
+
41
+ foreach ($ this ->data () as $ property => $ value ) {
42
+ $ self ->{$ property } = $ value ;
43
+ }
44
+
45
+ return $ self ;
46
+ }
47
+
48
+ public function create_simple_1 (): self {
49
+ $ self = new self ();
50
+
51
+ $ data = $ this ->data ();
52
+
53
+ foreach ($ data as $ property => $ value ) {
54
+ $ self ->{$ property } = $ value ;
55
+ }
56
+
57
+ return $ self ;
58
+ }
59
+
60
+ public function create_complex (): self {
61
+ $ self = new self ();
62
+
63
+ foreach ($ this ->data () as $ property => $ value ) {
64
+ if ($ property === 'test ' ) {
65
+ if ($ self ->{$ property } === null ) {
66
+ $ self ->{$ property } = new \stdClass ();
67
+ }
68
+ } else {
69
+ $ self ->{$ property } = $ value ;
70
+ }
71
+
72
+ if ($ property === 'foo ' ) {
73
+ $ self ->{$ property } += 1 ;
74
+ }
75
+ if ($ property === 'foo ' ) {
76
+ $ self ->{$ property } .= ' ' ;
77
+ }
78
+ if ($ property === 'lall ' ) {
79
+ $ self ->{$ property } += 1 ;
80
+ }
81
+ $ tmp = 1.1 ;
82
+ if ($ property === 'foo ' ) {
83
+ $ self ->{$ property } += $ tmp ;
84
+ }
85
+ }
86
+
87
+ return $ self ;
88
+ }
89
+
90
+ public function create_simple_2 (): self {
91
+ $ self = new self ();
92
+
93
+ $ data = $ this ->data ();
94
+
95
+ $ property = 'foo ' ;
96
+ foreach ($ data as $ value ) {
97
+ $ self ->{$ property } = $ value ;
98
+ }
99
+
100
+ return $ self ;
101
+ }
102
+
103
+ public function create_double_loop (): self {
104
+ $ self = new self ();
105
+
106
+ $ data = $ this ->data ();
107
+
108
+ foreach ($ data as $ property => $ value ) {
109
+ foreach ([1 , 2 , 3 ] as $ value_2 ) {
110
+ $ self ->{$ property } = $ value ;
111
+ }
112
+ }
113
+
114
+ return $ self ;
115
+ }
116
+ }
117
+
118
+
119
+ class FooBar
120
+ {
121
+ /**
122
+ * @var string
123
+ */
124
+ public $ foo = '' ;
125
+
126
+ /**
127
+ * @var null|\stdClass
128
+ */
129
+ public $ lall ;
130
+
131
+ public function data (): array
132
+ {
133
+ return ['foo ' => 'bar ' , 'lall ' => 'lall ' , 'noop ' => 1 ];
134
+ }
135
+
136
+ public function create (): self {
137
+ $ self = new self ();
138
+
139
+ foreach ($ this ->data () as $ property => $ value ) {
140
+ $ this ->{$ property } = $ value ;
141
+
142
+ if ($ property === 'lall ' ) {
143
+ $ this ->{$ property } = null ;
144
+ }
145
+
146
+ if ($ property === 'foo ' ) {
147
+ $ this ->{$ property } = 1.1 ;
148
+ }
149
+ }
150
+
151
+ return $ self ;
152
+ }
153
+ }
0 commit comments