Skip to content

Commit 0bfba41

Browse files
committed
refactor: switches to static what can be
1 parent 2fb4545 commit 0bfba41

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

src/Models/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,8 @@ public static function setInvalidArgumentException( string $class ) {
103103

104104
static::$invalidArgumentException = $class;
105105
}
106+
107+
public static function throwInvalidArgumentException( string $message ) {
108+
throw new static::$invalidArgumentException( $message );
109+
}
106110
}

src/Models/Contracts/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getOriginal( string $key = null );
6767
*
6868
* @return bool
6969
*/
70-
public function hasProperty( string $key ) : bool;
70+
public static function hasProperty( string $key ) : bool;
7171

7272
/**
7373
* Determines if a given attribute is clean.
@@ -101,7 +101,7 @@ public function isDirty( string $attribute = null ) : bool;
101101
*
102102
* @return bool
103103
*/
104-
public function isPropertyTypeValid( string $key, $value ) : bool;
104+
public static function isPropertyTypeValid( string $key, $value ) : bool;
105105

106106
/**
107107
* Returns the property keys.

src/Models/Model.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ abstract class Model implements ModelInterface, Arrayable, JsonSerializable {
2626
/**
2727
* The model properties assigned to their types.
2828
*
29-
* @var array<string,string>
29+
* @var array<string,string|array>
3030
*/
31-
protected $properties = [];
31+
static protected $properties = [];
3232

3333
/**
3434
* The model relationships assigned to their relationship types.
3535
*
3636
* @var array<string,string>
3737
*/
38-
protected $relationships = [];
38+
static protected $relationships = [];
3939

4040
/**
4141
* Relationships that have already been loaded and don't need to be loaded again.
@@ -52,7 +52,7 @@ abstract class Model implements ModelInterface, Arrayable, JsonSerializable {
5252
* @param array<string,mixed> $attributes Attributes.
5353
*/
5454
public function __construct( array $attributes = [] ) {
55-
$this->fill( array_merge( $this->getPropertyDefaults(), $attributes ) );
55+
$this->fill( array_merge( static::getPropertyDefaults(), $attributes ) );
5656

5757
$this->syncOriginal();
5858
}
@@ -133,7 +133,7 @@ public function getOriginal( string $key = null ) {
133133
* @return boolean
134134
*/
135135
public function isSet( string $key ): bool {
136-
return array_key_exists( $key, $this->attributes ) || $this->hasDefault( $key );
136+
return array_key_exists( $key, $this->attributes ) || static::hasDefault( $key );
137137
}
138138

139139
/**
@@ -145,8 +145,8 @@ public function isSet( string $key ): bool {
145145
*
146146
* @return bool
147147
*/
148-
protected function hasDefault( string $key ): bool {
149-
return is_array( $this->properties[ $key ] ) && array_key_exists( 1, $this->properties[ $key ] );
148+
protected static function hasDefault( string $key ): bool {
149+
return is_array( static::$properties[ $key ] ) && array_key_exists( 1, static::$properties[ $key ] );
150150
}
151151

152152
/**
@@ -158,9 +158,9 @@ protected function hasDefault( string $key ): bool {
158158
*
159159
* @return mixed|null
160160
*/
161-
protected function getPropertyDefault( string $key ) {
162-
if ( $this->hasDefault( $key ) ) {
163-
return $this->properties[ $key ][1];
161+
protected static function getPropertyDefault( string $key ) {
162+
if ( static::hasDefault( $key ) ) {
163+
return static::$properties[ $key ][1];
164164
}
165165

166166
return null;
@@ -173,11 +173,11 @@ protected function getPropertyDefault( string $key ) {
173173
*
174174
* @return array<string,mixed>
175175
*/
176-
protected function getPropertyDefaults() : array {
176+
protected static function getPropertyDefaults() : array {
177177
$defaults = [];
178-
foreach ( array_keys( $this->properties ) as $property ) {
179-
if ( $this->hasDefault( $property ) ) {
180-
$defaults[ $property ] = $this->getPropertyDefault( $property );
178+
foreach ( array_keys( static::$properties ) as $property ) {
179+
if ( static::hasDefault( $property ) ) {
180+
$defaults[ $property ] = static::getPropertyDefault( $property );
181181
}
182182
}
183183

@@ -194,7 +194,7 @@ protected function getPropertyDefaults() : array {
194194
* @return string
195195
*/
196196
protected function getPropertyType( string $key ) : string {
197-
$type = is_array( $this->properties[ $key ] ) ? $this->properties[ $key ][0] : $this->properties[ $key ];
197+
$type = is_array( static::$properties[ $key ] ) ? static::$properties[ $key ][0] : static::$properties[ $key ];
198198

199199
return strtolower( trim( $type ) );
200200
}
@@ -268,8 +268,8 @@ protected function hasCachedRelationship( string $key ) : bool {
268268
*
269269
* @return bool
270270
*/
271-
public function hasProperty( string $key ) : bool {
272-
return array_key_exists( $key, $this->properties );
271+
public static function hasProperty( string $key ) : bool {
272+
return array_key_exists( $key, static::$properties );
273273
}
274274

275275
/**
@@ -312,12 +312,12 @@ public function isDirty( string $attribute = null ) : bool {
312312
*
313313
* @return bool
314314
*/
315-
public function isPropertyTypeValid( string $key, $value ) : bool {
315+
public static function isPropertyTypeValid( string $key, $value ) : bool {
316316
if ( is_null( $value ) ) {
317317
return true;
318318
}
319319

320-
$type = $this->getPropertyType( $key );
320+
$type = static::getPropertyType( $key );
321321

322322
switch ( $type ) {
323323
case 'int':
@@ -357,7 +357,7 @@ public function jsonSerialize() {
357357
* @return int[]|string[]
358358
*/
359359
public static function propertyKeys() : array {
360-
return array_keys( ( new static() )->properties );
360+
return array_keys( static::$properties );
361361
}
362362

363363
/**
@@ -451,9 +451,9 @@ protected function validatePropertyExists( string $key ) {
451451
*
452452
* @return void
453453
*/
454-
protected function validatePropertyType( string $key, $value ) {
455-
if ( ! $this->isPropertyTypeValid( $key, $value ) ) {
456-
$type = $this->getPropertyType( $key );
454+
protected static function validatePropertyType( string $key, $value ) {
455+
if ( ! static::isPropertyTypeValid( $key, $value ) ) {
456+
$type = static::getPropertyType( $key );
457457

458458
$exception = Config::getInvalidArgumentException();
459459
throw new $exception( "Invalid attribute assignment. '$key' should be of type: '$type'" );
@@ -470,7 +470,7 @@ protected function validatePropertyType( string $key, $value ) {
470470
* @return mixed
471471
*/
472472
public function __get( string $key ) {
473-
if ( array_key_exists( $key, $this->relationships ) ) {
473+
if ( array_key_exists( $key, static::$relationships ) ) {
474474
return $this->getRelationship( $key );
475475
}
476476

0 commit comments

Comments
 (0)