Skip to content

Commit 8d4074b

Browse files
authored
Merge pull request #85 from samsonasik/apply-php80
Apply PHP 8.0 Syntax and constructor promotion
2 parents f16c293 + fc11936 commit 8d4074b

19 files changed

+54
-96
lines changed

src/CodeGenerator/AbstractInjector.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ abstract class AbstractInjector implements InjectorInterface
1717
protected $factories = [];
1818

1919
/** @var array<string, FactoryInterface> */
20-
private $factoryInstances = [];
20+
private array $factoryInstances = [];
2121

2222
private ContainerInterface $container;
2323

24-
private InjectorInterface $injector;
25-
26-
public function __construct(InjectorInterface $injector, ?ContainerInterface $container = null)
24+
public function __construct(private InjectorInterface $injector, ?ContainerInterface $container = null)
2725
{
28-
$this->injector = $injector;
2926
$this->container = $container ?: new DefaultContainer($this);
3027

3128
$this->loadFactoryList();

src/CodeGenerator/AutoloadGenerator.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ class AutoloadGenerator
2626
private const CLASS_TEMPLATE = __DIR__ . '/../../templates/autoloader-class.template';
2727
private const FILE_TEMPLATE = __DIR__ . '/../../templates/autoloader-file.template';
2828

29-
private string $namespace;
30-
31-
public function __construct(string $namespace)
29+
public function __construct(private string $namespace)
3230
{
33-
$this->namespace = $namespace;
3431
}
3532

3633
private function writeFile(string $filename, string $code): void
@@ -65,7 +62,7 @@ private function buildFromTemplate(string $templateFile, string $outputFile, arr
6562
private function generateClassmapCode(array &$classmap): string
6663
{
6764
$lines = array_map(
68-
fn(string $class, string $file): string => sprintf(
65+
static fn(string $class, string $file): string => sprintf(
6966
'%s => %s,',
7067
var_export($class, true),
7168
var_export($file, true)

src/CodeGenerator/FactoryGenerator.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,14 @@ class FactoryGenerator
4848

4949
private string $namespace;
5050

51-
private DependencyResolverInterface $resolver;
52-
53-
private ConfigInterface $config;
54-
5551
/** @var array<string, string> */
5652
private array $classmap = [];
5753

5854
public function __construct(
59-
ConfigInterface $config,
60-
DependencyResolverInterface $resolver,
55+
private ConfigInterface $config,
56+
private DependencyResolverInterface $resolver,
6157
?string $namespace = null
6258
) {
63-
$this->resolver = $resolver;
64-
$this->config = $config;
6559
$this->namespace = $namespace ?: 'LaminasDiGenerated';
6660
}
6761

src/CodeGenerator/InjectorGenerator.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ class InjectorGenerator
4040
private const INJECTOR_TEMPLATE = __DIR__ . '/../../templates/injector.template';
4141
private const INDENTATION_SPACES = 4;
4242

43-
private ConfigInterface $config;
44-
4543
/**
4644
* @deprecated
4745
*
@@ -68,12 +66,11 @@ class InjectorGenerator
6866
* and processed classes.
6967
*/
7068
public function __construct(
71-
ConfigInterface $config,
69+
private ConfigInterface $config,
7270
DependencyResolverInterface $resolver,
7371
?string $namespace = null,
7472
?LoggerInterface $logger = null
7573
) {
76-
$this->config = $config;
7774
$this->namespace = $namespace ? : 'Laminas\Di\Generated';
7875
$this->factoryGenerator = new FactoryGenerator($config, $resolver, $this->namespace . '\Factory');
7976
$this->autoloadGenerator = new AutoloadGenerator($this->namespace);
@@ -113,7 +110,7 @@ private function generateFactoryList(array $factories): void
113110
{
114111
$indentation = sprintf("\n%s", str_repeat(' ', self::INDENTATION_SPACES));
115112
$codeLines = array_map(
116-
fn(string $key, string $value): string =>
113+
static fn(string $key, string $value): string =>
117114
sprintf('%s => %s,', var_export($key, true), var_export($value, true)),
118115
array_keys($factories),
119116
$factories
@@ -154,7 +151,7 @@ private function generateTypeFactory(string $class, array &$factories): void
154151

155152
private function generateAutoload(): void
156153
{
157-
$addFactoryPrefix = fn(string $value): string => 'Factory/' . $value;
154+
$addFactoryPrefix = static fn(string $value): string => 'Factory/' . $value;
158155

159156
$classmap = array_map($addFactoryPrefix, $this->factoryGenerator->getClassmap());
160157

src/Config.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,8 @@ public function setAlias(string $name, string $class): self
249249

250250
/**
251251
* @psalm-assert array|ArrayAccess $options
252-
* @param mixed $options
253252
*/
254-
private function ensureArrayOrArrayAccess($options): void
253+
private function ensureArrayOrArrayAccess(mixed $options): void
255254
{
256255
if (! is_array($options) && ! $options instanceof ArrayAccess) {
257256
throw new Exception\InvalidArgumentException(

src/Container/ConfigFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
use Laminas\Di\ConfigInterface;
1010
use Laminas\Di\LegacyConfig;
1111
use Psr\Container\ContainerInterface;
12-
use Traversable;
1312

1413
use function array_merge_recursive;
1514
use function assert;
1615
use function is_array;
16+
use function is_iterable;
1717
use function trigger_error;
1818

1919
use const E_USER_DEPRECATED;
@@ -47,7 +47,7 @@ public function create(ContainerInterface $container): ConfigInterface
4747
E_USER_DEPRECATED
4848
);
4949

50-
assert(is_array($legacyData) || $legacyData instanceof Traversable || $legacyData instanceof ArrayAccess);
50+
assert(is_iterable($legacyData) || $legacyData instanceof ArrayAccess);
5151

5252
$legacyConfig = new LegacyConfig($legacyData);
5353
$data = array_merge_recursive($legacyConfig->toArray(), $data);

src/LegacyConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use function is_array;
1616
use function is_iterable;
1717
use function is_string;
18-
use function strpos;
18+
use function str_contains;
1919
use function trigger_error;
2020

2121
use const E_USER_DEPRECATED;
@@ -58,7 +58,7 @@ private function prepareParametersArray($parameters): array
5858
foreach ($parameters as $key => $value) {
5959
$key = (string) $key;
6060

61-
if (strpos($key, ':') !== false) {
61+
if (str_contains($key, ':')) {
6262
trigger_error('Full qualified parameter positions are no longer supported', E_USER_DEPRECATED);
6363
}
6464

src/Resolver/DependencyResolver.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function getConfiguredParameters(string $requestedType): array
107107

108108
// A type configuration may define a parameter should be auto resolved
109109
// even it was defined earlier
110-
$params = array_filter($params, fn($value) => $value !== '*');
110+
$params = array_filter($params, static fn($value): bool => $value !== '*');
111111

112112
return $params;
113113
}
@@ -148,10 +148,7 @@ private function isUsableType(string $type, string $requiredType): bool
148148
&& ($this->container === null || $this->container->has($type));
149149
}
150150

151-
/**
152-
* @param mixed $value
153-
*/
154-
private function getTypeNameFromValue($value): string
151+
private function getTypeNameFromValue(mixed $value): string
155152
{
156153
$type = gettype($value);
157154
return $this->gettypeMap[$type] ?? $type;
@@ -163,7 +160,7 @@ private function getTypeNameFromValue($value): string
163160
* @param mixed $value The value to check
164161
* @param string $type The typename to check against
165162
*/
166-
private function isValueOf($value, string $type): bool
163+
private function isValueOf(mixed $value, string $type): bool
167164
{
168165
if (! $this->isBuiltinType($type)) {
169166
return $value instanceof $type;
@@ -224,10 +221,8 @@ private function isCallableType(string $type): bool
224221
* Prepare a candidate for injection
225222
*
226223
* If the candidate is usable, its injection representation is returned
227-
*
228-
* @param mixed $value
229224
*/
230-
private function prepareInjection($value, ?string $requiredType): ?InjectionInterface
225+
private function prepareInjection(mixed $value, ?string $requiredType): ?InjectionInterface
231226
{
232227
if ($value instanceof ValueInjection || $value instanceof TypeInjection) {
233228
return $value;

src/Resolver/TypeInjection.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Laminas\Di\Resolver;
66

77
use Psr\Container\ContainerInterface;
8+
use Stringable;
89

910
use function trigger_error;
1011
use function var_export;
@@ -14,19 +15,17 @@
1415
/**
1516
* Wrapper for types that should be looked up for injection
1617
*/
17-
final class TypeInjection implements InjectionInterface
18+
final class TypeInjection implements InjectionInterface, Stringable
1819
{
19-
/**
20-
* Holds the type name to look up
21-
*/
22-
private string $type;
23-
2420
/**
2521
* Constructor
2622
*/
27-
public function __construct(string $type)
28-
{
29-
$this->type = $type;
23+
public function __construct(
24+
/**
25+
* Holds the type name to look up
26+
*/
27+
private string $type
28+
) {
3029
}
3130

3231
public function export(): string

src/Resolver/ValueInjection.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,12 @@
2222
*/
2323
class ValueInjection implements InjectionInterface
2424
{
25-
/**
26-
* Holds the value to inject
27-
*
28-
* @var mixed
29-
*/
30-
protected $value;
31-
32-
/**
33-
* @param mixed $value
34-
*/
35-
public function __construct($value)
36-
{
37-
$this->value = $value;
25+
public function __construct(
26+
/**
27+
* Holds the value to inject
28+
*/
29+
protected mixed $value
30+
) {
3831
}
3932

4033
public static function __set_state(array $state): self
@@ -71,10 +64,8 @@ public function isExportable(): bool
7164
/**
7265
* Check if the provided value is exportable.
7366
* For arrays it uses recursion.
74-
*
75-
* @param mixed $value
7667
*/
77-
private function isExportableRecursive($value): bool
68+
private function isExportableRecursive(mixed $value): bool
7869
{
7970
if (is_scalar($value) || $value === null) {
8071
return true;

0 commit comments

Comments
 (0)