11<?php
22/**
33 * This file is part of event-engine/php-data.
4- * (c) 2018-2019 prooph software GmbH <contact@prooph.de>
4+ * (c) 2018-2020 prooph software GmbH <contact@prooph.de>
55 *
66 * For the full copyright and license information, please view the LICENSE
77 * file that was distributed with this source code.
1111
1212namespace EventEngine \Data ;
1313
14+ /**
15+ * Trait ImmutableRecordLogic
16+ * @package EventEngine\Data
17+ *
18+ * @psalm-immutable
19+ */
1420trait ImmutableRecordLogic
1521{
1622 /**
@@ -36,7 +42,7 @@ private function init(): void
3642 * @param array $recordData
3743 * @return self
3844 */
39- public static function fromRecordData (array $ recordData )
45+ public static function fromRecordData (array $ recordData ): self
4046 {
4147 return new self ($ recordData );
4248 }
@@ -45,7 +51,7 @@ public static function fromRecordData(array $recordData)
4551 * @param array $nativeData
4652 * @return self
4753 */
48- public static function fromArray (array $ nativeData )
54+ public static function fromArray (array $ nativeData ): self
4955 {
5056 return new self (null , $ nativeData );
5157 }
@@ -73,7 +79,7 @@ private function __construct(array $recordData = null, array $nativeData = null)
7379 * @param array $recordData
7480 * @return self
7581 */
76- public function with (array $ recordData )
82+ public function with (array $ recordData ): self
7783 {
7884 $ copy = clone $ this ;
7985 $ copy ->setRecordData ($ recordData );
@@ -94,24 +100,24 @@ public function toArray(): array
94100 case ImmutableRecord::PHP_TYPE_BOOL :
95101 case ImmutableRecord::PHP_TYPE_ARRAY :
96102 if (\array_key_exists ($ key , $ arrayPropItemTypeMap ) && ! self ::isScalarType ($ arrayPropItemTypeMap [$ key ])) {
97- if ($ isNullable && $ this ->{$ key }() === null ) {
103+ if ($ isNullable && $ this ->{$ key } === null ) {
98104 $ nativeData [$ key ] = null ;
99105 continue 2 ;
100106 }
101107
102108 $ nativeData [$ key ] = \array_map (function ($ item ) use ($ key , &$ arrayPropItemTypeMap ) {
103109 return $ this ->voTypeToNative ($ item , $ key , $ arrayPropItemTypeMap [$ key ]);
104- }, $ this ->{$ key }() );
110+ }, $ this ->{$ key });
105111 } else {
106- $ nativeData [$ key ] = $ this ->{$ key }() ;
112+ $ nativeData [$ key ] = $ this ->{$ key };
107113 }
108114 break ;
109115 default :
110- if ($ isNullable && $ this ->{$ key }() === null ) {
116+ if ($ isNullable && (! isset ( $ this ->{$ key })) ) {
111117 $ nativeData [$ key ] = null ;
112118 continue 2 ;
113119 }
114- $ nativeData [$ key ] = $ this ->voTypeToNative ($ this ->{$ key }() , $ key , $ type );
120+ $ nativeData [$ key ] = $ this ->voTypeToNative ($ this ->{$ key }, $ key , $ type );
115121 }
116122 }
117123
@@ -120,22 +126,22 @@ public function toArray(): array
120126
121127 public function equals (ImmutableRecord $ other ): bool
122128 {
123- if (get_class ($ this ) !== get_class ($ other )) {
129+ if (get_class ($ this ) !== get_class ($ other )) {
124130 return false ;
125131 }
126132
127133 return $ this ->toArray () === $ other ->toArray ();
128134 }
129135
130- private function setRecordData (array $ recordData )
136+ private function setRecordData (array $ recordData ): void
131137 {
132138 foreach ($ recordData as $ key => $ value ) {
133139 $ this ->assertType ($ key , $ value );
134140 $ this ->{$ key } = $ value ;
135141 }
136142 }
137143
138- private function setNativeData (array $ nativeData )
144+ private function setNativeData (array $ nativeData ): void
139145 {
140146 $ recordData = [];
141147 $ arrayPropItemTypeMap = self ::getArrayPropItemTypeMapFromMethodOrCache ();
@@ -182,10 +188,10 @@ private function setNativeData(array $nativeData)
182188 $ this ->setRecordData ($ recordData );
183189 }
184190
185- private function assertAllNotNull ()
191+ private function assertAllNotNull (): void
186192 {
187193 foreach (self ::$ __propTypeMap as $ key => [$ type , $ isNative , $ isNullable ]) {
188- if (null === $ this ->{$ key } && ! $ isNullable ) {
194+ if (! isset ( $ this ->{$ key }) && ! $ isNullable ) {
189195 throw new \InvalidArgumentException (\sprintf (
190196 'Missing record data for key %s of record %s. ' ,
191197 $ key ,
@@ -195,7 +201,7 @@ private function assertAllNotNull()
195201 }
196202 }
197203
198- private function assertType (string $ key , $ value )
204+ private function assertType (string $ key , $ value ): void
199205 {
200206 if (! isset (self ::$ __propTypeMap [$ key ])) {
201207 throw new \InvalidArgumentException (\sprintf (
@@ -264,7 +270,7 @@ private function isType(string $type, string $key, $value): bool
264270 }
265271 }
266272
267- private static function buildPropTypeMap ()
273+ private static function buildPropTypeMap (): array
268274 {
269275 $ refObj = new \ReflectionClass (__CLASS__ );
270276
@@ -383,12 +389,12 @@ private static function getArrayPropItemTypeMapFromMethodOrCache(): array
383389 }
384390
385391 /**
386- * @var array
392+ * @var array|null
387393 */
388394 private static $ __propTypeMap ;
389395
390396 /**
391- * @var array
397+ * @var array|null
392398 */
393399 private static $ __arrayPropItemTypeMap ;
394400}
0 commit comments