Skip to content

Commit 59b61e8

Browse files
committed
Fixed some IDE warnings and reformat codes
1 parent acc5537 commit 59b61e8

File tree

6 files changed

+74
-74
lines changed

6 files changed

+74
-74
lines changed

tests/Casts/StateCaster.php

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,45 @@
22

33
namespace Kodeine\Metable\Tests\Casts;
44

5+
use Exception;
56
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
67

78
class StateCaster implements CastsAttributes
89
{
9-
private string $baseStateClass;
10-
11-
public function __construct(string $baseStateClass)
12-
{
13-
$this->baseStateClass = $baseStateClass;
14-
}
15-
10+
private $baseStateClass;
11+
12+
public function __construct(string $baseStateClass) {
13+
$this->baseStateClass = $baseStateClass;
14+
}
15+
1616
public function get($model, $key, $value, $attributes) {
1717
if ( is_null( $value ) ) return null;
18-
19-
if (! is_subclass_of($value, $this->baseStateClass)) {
18+
19+
if ( ! is_subclass_of( $value, $this->baseStateClass ) ) {
2020
return null;
2121
}
22-
22+
2323
$stateClassName = $value::config()['default'];
24-
25-
$state = new $stateClassName($model);
26-
27-
return $state;
24+
25+
return new $stateClassName( $model );
2826
}
2927

28+
/**
29+
* @throws Exception
30+
*/
3031
public function set($model, $key, $value, $attributes) {
3132
if ( is_null( $value ) ) return null;
32-
33-
if (! is_subclass_of($value, $this->baseStateClass)) {
34-
throw new \Exception('Invalid state class.');
33+
34+
if ( ! is_subclass_of( $value, $this->baseStateClass ) ) {
35+
throw new Exception( 'Invalid state class.' );
3536
}
36-
37-
$value = new $value($model);
38-
39-
if ($value instanceof $this->baseStateClass) {
40-
$value->setField($key);
37+
38+
$value = new $value( $model );
39+
40+
if ( $value instanceof $this->baseStateClass ) {
41+
$value->setField( $key );
4142
}
42-
43+
4344
return $value->getMorphClass();
4445
}
4546
}

tests/Casts/UserCastedObject.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,41 @@
66

77
abstract class UserCastedObject implements Castable
88
{
9+
public static $name;
10+
public $model;
11+
public $stateConfig;
12+
public $field;
13+
914
public function __construct($model) {
1015
$this->model = $model;
1116
$this->stateConfig = static::config();
1217
}
13-
14-
public static function config()
15-
{
18+
19+
public static function config() {
1620
return [
1721
'default' => null,
1822
];
1923
}
2024

2125
public static function castUsing(array $arguments) {
22-
return new StateCaster(static::class);
26+
return new StateCaster( static::class );
2327
}
24-
25-
public function setField(string $field): self
26-
{
27-
$this->field = $field;
28-
29-
return $this;
30-
}
31-
32-
public static function getMorphClass(): string
33-
{
34-
return static::$name ?? static::class;
35-
}
36-
37-
public function make(string $name, $model)
38-
{
39-
if (is_null($name)) {
28+
29+
public function setField(string $field): self {
30+
$this->field = $field;
31+
32+
return $this;
33+
}
34+
35+
public static function getMorphClass(): string {
36+
return static::$name ?? static::class;
37+
}
38+
39+
public function make(?string $name, $model) {
40+
if ( is_null( $name ) ) {
4041
return null;
4142
}
42-
43-
return new $name($model);
43+
44+
return new $name( $model );
4445
}
4546
}

tests/Casts/UserState/DefaultState.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
class DefaultState extends State
66
{
7-
public string $description;
7+
public $description;
88

9+
/** @noinspection PhpMissingParentConstructorInspection */
910
public function __construct() {
1011
$this->description = $this->description();
1112
}
12-
13+
1314
public function description(): string {
1415
return 'This is a default description.';
1516
}

tests/MetableTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public static function setUpBeforeClass(): void {
5252

5353
public function testCast() {
5454
$user = new UserTest;
55-
55+
5656
$this->assertNull( $user->state, 'Casted object should be null by default' );
57-
57+
5858
$user->state = DefaultState::class;
59-
60-
$this->assertTrue( $user->state instanceof DefaultState, 'Casted object should be instanceof DefaultState' );
59+
60+
$this->assertInstanceOf( DefaultState::class, $user->state, 'Casted object should be instanceof DefaultState' );
6161
}
62-
62+
6363
public function testFluentMeta() {
6464
$user = new UserTest;
6565

tests/Models/UserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class UserTest extends Model
2121
public $hideMeta = false;
2222

2323
public $disableFluentMeta = false;
24-
24+
2525
protected $casts = [
2626
'state' => State::class,
2727
];

tests/Traits/HasUserStates.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,50 @@
33
namespace Kodeine\Metable\Tests\Traits;
44

55
use Kodeine\Metable\Tests\Casts\UserState\State;
6+
use Kodeine\Metable\Tests\Casts\UserCastedObject;
67

78
trait HasUserStates
89
{
910
public $casted = [
1011
'state' => State::class,
1112
];
12-
13-
public static function bootHasUserCasts()
14-
{
15-
self::creating( function ( $model ) {
13+
14+
public static function bootHasUserCasts() {
15+
self::creating( function ($model) {
1616
$model->setDefaultCastedProperties();
1717
} );
1818
}
19-
20-
public function initializeHasUserCasts()
21-
{
19+
20+
public function initializeHasUserCasts() {
2221
$this->setDefaultCastedProperties();
2322
}
24-
25-
private function getStateConfigs()
26-
{
23+
24+
private function getStateConfigs() {
2725
$casts = $this->getCasts();
2826

2927
$states = [];
30-
28+
3129
foreach ($casts as $prop => $state) {
32-
if (! is_subclass_of($state, UserCastedObject::class)) {
30+
if ( ! is_subclass_of( $state, UserCastedObject::class ) ) {
3331
continue;
3432
}
35-
33+
3634
$states[$prop] = $state::config();
3735
}
38-
36+
3937
return $states;
4038
}
41-
42-
private function setDefaultCastedProperties()
43-
{
44-
foreach ( $this->getStateConfigs() as $prop => $config ) {
45-
if ($this->{$prop} === null) {
39+
40+
private function setDefaultCastedProperties() {
41+
foreach ($this->getStateConfigs() as $prop => $config) {
42+
if ( $this->{$prop} === null ) {
4643
continue;
4744
}
48-
45+
4946
if ( ! isset( $config['default'] ) ) {
5047
continue;
5148
}
52-
49+
5350
$this->{$prop} = $config['default'];
5451
}
5552
}

0 commit comments

Comments
 (0)