Skip to content

Commit 40b3ee1

Browse files
Marvin Durotmarvindurot
Marvin Durot
authored andcommitted
Upgrade to PHP 8.1 with Rector
1 parent 5051e6a commit 40b3ee1

15 files changed

+44
-126
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010
],
1111
"require" : {
12-
"php" : ">=8.0",
12+
"php" : ">=8.1",
1313
"php-curl-class/php-curl-class": "^8.3",
1414
"psr/cache": "^1.0 || ^2.0 || ^3.0"
1515
},

rector.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__DIR__ . '/src',
1010
__DIR__ . '/tests',
1111
])
12-
->withPhpSets(php80: true)
12+
->withPhpSets(php81: true)
1313
->withTypeCoverageLevel(0)
1414
->withDeadCodeLevel(0)
1515
->withCodeQualityLevel(0);

src/Bridges/BarryvdhLaravelDebugbar/DebugbarTransportDecorator.php

+1-13
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ class DebugbarTransportDecorator implements TransportInterface
1717
*/
1818
const DEBUG_COLLECTOR = 'api';
1919

20-
/**
21-
* @var TransportInterface
22-
*/
23-
private $transport;
24-
25-
/**
26-
* @var string
27-
*/
28-
private $name;
29-
3020
/**
3121
* @var LaravelDebugbar
3222
*/
@@ -39,10 +29,8 @@ class DebugbarTransportDecorator implements TransportInterface
3929
* @param string $name
4030
* @param LaravelDebugbar $debugbar
4131
*/
42-
public function __construct(TransportInterface $transport, string $name, ?LaravelDebugbar $debugbar)
32+
public function __construct(private TransportInterface $transport, private string $name, ?LaravelDebugbar $debugbar)
4333
{
44-
$this->transport = $transport;
45-
$this->name = $name;
4634
$this->debugbar = $debugbar;
4735
}
4836

src/Bridges/Laravel/HasApiRelations.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function getRelationshipFromMethod($method)
8282
$relation = $this->$method();
8383

8484
if (!($relation instanceof Relation || $relation instanceof RelationApi)) {
85-
throw new LogicException(\get_class($this).'::'.$method.' must return a relationship instance.');
85+
throw new LogicException($this::class.'::'.$method.' must return a relationship instance.');
8686
}
8787

8888
return tap($relation->getResults(), function ($results) use ($method): void {

src/Bridges/Symfony/Adapter/ApiWrapperPaginatorAdapter.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@
77

88
class ApiWrapperPaginatorAdapter implements \IteratorAggregate, ApiPlatformPaginator
99
{
10-
/**
11-
* @var PaginatorInterface
12-
*/
13-
private $paginator;
14-
15-
public function __construct(PaginatorInterface $paginator)
10+
public function __construct(private PaginatorInterface $paginator)
1611
{
17-
$this->paginator = $paginator;
1812
}
1913

2014
/**

src/Bridges/Symfony/DataProvider/ApiPlatformDataProvider.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@
1212

1313
final class ApiPlatformDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface
1414
{
15-
/**
16-
* @var ManagerRegistry
17-
*/
18-
private $managerRegistry;
19-
20-
public function __construct(ManagerRegistry $managerRegistry)
15+
public function __construct(private ManagerRegistry $managerRegistry)
2116
{
22-
$this->managerRegistry = $managerRegistry;
2317
}
2418

2519
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool

src/Bridges/Symfony/Paginator.php

+4-17
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,13 @@ class Paginator implements PaginatorInterface
1818
private $data;
1919

2020
/**
21-
* @var float|null
21+
* @param float|null $total
22+
* @param float|null $itemPerPage
23+
* @param float|null $currentPage
2224
*/
23-
private $total;
24-
25-
/**
26-
* @var float|null
27-
*/
28-
private $itemPerPage;
29-
30-
/**
31-
* @var float|null
32-
*/
33-
private $currentPage;
34-
35-
public function __construct($data, $total, $itemPerPage = null, $currentPage = null)
25+
public function __construct($data, private $total, private $itemPerPage = null, private $currentPage = null)
3626
{
3727
$this->data = new \ArrayIterator($data);
38-
$this->total = $total;
39-
$this->itemPerPage = $itemPerPage;
40-
$this->currentPage = $currentPage;
4128
}
4229

4330
/**

src/Builder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function find($field, $value = null)
8787

8888
try {
8989
$res = $this->findOrFail($field, $value);
90-
} catch (ApiEntityNotFoundException $e) {
90+
} catch (ApiEntityNotFoundException) {
9191
return null;
9292
}
9393

@@ -261,7 +261,7 @@ public function raw()
261261
$instance = $this->getModel();
262262
try {
263263
return $instance->getApi()->{'get'.ucfirst($instance->getEntities())}($this->getQuery());
264-
} catch (ApiEntityNotFoundException $e) {
264+
} catch (ApiEntityNotFoundException) {
265265
return [];
266266
}
267267
}

src/Concerns/HasAttributes.php

+13-28
Original file line numberDiff line numberDiff line change
@@ -110,33 +110,18 @@ protected function castAttribute($key, $value)
110110
return $value;
111111
}
112112

113-
switch ($this->getCastType($key)) {
114-
case 'int':
115-
case 'integer':
116-
return (int) $value;
117-
case 'real':
118-
case 'float':
119-
case 'double':
120-
return (float) $value;
121-
case 'string':
122-
return (string) $value;
123-
case 'bool':
124-
case 'boolean':
125-
return (bool) $value;
126-
case 'object':
127-
return $this->fromJson($value, true);
128-
case 'array':
129-
case 'json':
130-
return $this->fromJson($value);
131-
case 'date':
132-
return $this->asDate($value);
133-
case 'datetime':
134-
return $this->asDateTime($value);
135-
case 'timestamp':
136-
return $this->asTimestamp($value);
137-
default:
138-
return $this->asModel($key, $value) ?? $value;
139-
}
113+
return match ($this->getCastType($key)) {
114+
'int', 'integer' => (int) $value,
115+
'real', 'float', 'double' => (float) $value,
116+
'string' => (string) $value,
117+
'bool', 'boolean' => (bool) $value,
118+
'object' => $this->fromJson($value, true),
119+
'array', 'json' => $this->fromJson($value),
120+
'date' => $this->asDate($value),
121+
'datetime' => $this->asDateTime($value),
122+
'timestamp' => $this->asTimestamp($value),
123+
default => $this->asModel($key, $value) ?? $value,
124+
};
140125
}
141126

142127
/**
@@ -288,7 +273,7 @@ protected function getRelationshipFromMethod($method)
288273
$relation = $this->$method();
289274

290275
if (!$relation instanceof RelationInterface) {
291-
throw new LogicException(get_class($this).'::'.$method.' must return a relationship instance.');
276+
throw new LogicException($this::class.'::'.$method.' must return a relationship instance.');
292277
}
293278

294279
$results = $relation->getResults();

src/Exceptions/ApiException.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77

88
class ApiException extends Exception
99
{
10-
protected $response;
11-
1210
/**
1311
* @var string|null Can be used to specify from which API the exception has been thrown.
1412
*/
1513
protected $source;
1614

17-
public function __construct($response, $message = "", $httpCode = 0, Throwable $previous = null)
15+
public function __construct(protected $response, $message = "", $httpCode = 0, Throwable $previous = null)
1816
{
1917
parent::__construct($message, $httpCode, $previous);
20-
$this->response = $response;
2118
}
2219

2320
/**

src/Model.php

+10-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Exception;
1414
use JsonSerializable;
1515

16-
abstract class Model implements ArrayAccess, JsonSerializable
16+
abstract class Model implements ArrayAccess, JsonSerializable, \Stringable
1717
{
1818
use HasAttributes;
1919
use HasRelationships;
@@ -48,13 +48,6 @@ abstract class Model implements ArrayAccess, JsonSerializable
4848
*/
4949
protected static $api = 'default';
5050

51-
/**
52-
* Indicates if the model exists.
53-
*
54-
* @var bool
55-
*/
56-
public $exists = false;
57-
5851
/**
5952
* The array of global scopes on the model.
6053
*
@@ -116,16 +109,21 @@ public function getEntity(): ?string
116109
*/
117110
public function getEntities(): string
118111
{
119-
if (substr($this->entity, -1) === 'y') {
112+
if (str_ends_with($this->entity, 'y')) {
120113
return rtrim($this->entity, 'y').'ies';
121114
}
122115

123116
return rtrim($this->entity, 's').'s';
124117
}
125118

126-
public function __construct($fill = [], $exists = false)
119+
/**
120+
* @param bool $exists
121+
*/
122+
public function __construct($fill = [], /**
123+
* Indicates if the model exists.
124+
*/
125+
public $exists = false)
127126
{
128-
$this->exists = $exists;
129127
$this->fill($fill);
130128
$this->syncOriginal();
131129
$this->boot();
@@ -278,7 +276,7 @@ public function __unset($key)
278276
*
279277
* @throws \Exception
280278
*/
281-
public function __toString()
279+
public function __toString(): string
282280
{
283281
return $this->toJson();
284282
}

src/MultipartParam.php

+3-19
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,15 @@
44

55
class MultipartParam
66
{
7-
/**
8-
* @var array|string
9-
*/
10-
protected $content;
11-
12-
/**
13-
* @var string|null
14-
*/
15-
protected $mimeType;
16-
17-
/**
18-
* @var string|null
19-
*/
20-
protected $filename;
21-
227
/**
238
* MultipartParam constructor.
249
*
2510
* @param array|string $content
11+
* @param string|null $mimeType
12+
* @param string|null $filename
2613
*/
27-
public function __construct($content, $mimeType = null, $filename = null)
14+
public function __construct(protected $content, protected $mimeType = null, protected $filename = null)
2815
{
29-
$this->content = $content;
30-
$this->mimeType = $mimeType;
31-
$this->filename = $filename;
3216
}
3317

3418
/**

src/Relations/BelongsTo.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@
66

77
class BelongsTo extends Relation
88
{
9-
protected $foreignKey;
10-
protected $localKey;
11-
12-
public function __construct(Model $parent, Model $related, $foreignKey, $localKey)
9+
public function __construct(Model $parent, Model $related, protected $foreignKey, protected $localKey)
1310
{
1411
parent::__construct($parent);
1512
$this->parent = $parent;
1613
$this->related = $related;
17-
$this->foreignKey = $foreignKey;
18-
$this->localKey = $localKey;
1914

2015
$this->addConstraints();
2116
}
@@ -53,7 +48,7 @@ public function addConstraints()
5348
*/
5449
public function getRelationsFromArray($data)
5550
{
56-
$class = get_class($this->related);
51+
$class = $this->related::class;
5752

5853
return new $class($data, isset($data[$this->localKey]));
5954
}

src/Relations/HasMany.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66

77
class HasMany extends Relation
88
{
9-
protected $foreignKey;
10-
protected $localKey;
119
protected $queryKey;
1210
protected $queryValue;
1311

14-
public function __construct(Model $parent, Model $related, $foreignKey, $localKey)
12+
public function __construct(Model $parent, Model $related, protected $foreignKey, protected $localKey)
1513
{
1614
parent::__construct($parent);
1715
$this->related = $related;
18-
$this->foreignKey = $foreignKey;
19-
$this->localKey = $localKey;
2016

2117
$this->addConstraints();
2218
}
@@ -43,7 +39,7 @@ public function addConstraints()
4339
*/
4440
public function getRelationsFromArray($data)
4541
{
46-
$class = get_class($this->related);
42+
$class = $this->related::class;
4743

4844
return array_map(fn($item) => new $class($item, isset($item[$this->localKey])), $data);
4945
}

src/Relations/HasOne.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function getResults()
2727
*/
2828
public function getRelationsFromArray($data)
2929
{
30-
$class = get_class($this->related);
30+
$class = $this->related::class;
3131

3232
return new $class($data, isset($data[$this->localKey]));
3333
}

0 commit comments

Comments
 (0)