AsService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 31
rs 9.9666
cc 1
nc 1
nop 16

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2024 Mykhailo Shtanko [email protected]
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15
16
namespace FRZB\Component\DependencyInjection\Attribute;
17
18
use Fp\Collections\ArrayList;
19
use Fp\Collections\HashMap;
20
use FRZB\Component\DependencyInjection\Exception\AttributeException;
21
use FRZB\Component\DependencyInjection\Helper\StringHelper;
22
use JetBrains\PhpStorm\Immutable;
23
24
#[Immutable]
25
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
26
final class AsService
27
{
28
    private const PARAMETER_PREFIX = '$';
29
30
    public readonly array $arguments;
31
32
    public function __construct(
33
        public readonly bool $isShared = true,
34
        public readonly bool $isSynthetic = false,
35
        public readonly bool $isLazy = true,
36
        public readonly bool $isPublic = true,
37
        public readonly bool $isAbstract = false,
38
        public readonly ?string $id = null,
39
        public readonly null|array|string $factory = null,
40
        public readonly ?string $file = null,
41
        array $arguments = [],
42
        public readonly array $properties = [],
43
        public readonly null|array|string $configurator = null,
44
        public readonly array $calls = [],
45
        /** @var AsTagged[] */
46
        public readonly array $tags = [],
47
        public readonly bool $isAutowired = true,
48
        public readonly bool $isAutoconfigured = true,
49
        public readonly array $bindings = [],
50
    ) {
51
        $this->arguments = HashMap::collect($arguments)
0 ignored issues
show
Bug introduced by
The property arguments is declared read-only in FRZB\Component\Dependenc...ion\Attribute\AsService.
Loading history...
52
            ->flatMapKV(static fn (mixed $key, mixed $value) => [StringHelper::toCamelCase($key) => $value])
53
            ->flatMapKV(static fn (string $key, array|string $value) => match (true) {
54
                str_contains($key, self::PARAMETER_PREFIX) => [$key => $value],
55
                default => [self::PARAMETER_PREFIX.$key => $value],
56
            })
57
            ->toArray()
58
        ;
59
60
        ArrayList::collect($this->tags)
61
            ->filter(static fn (mixed $tag) => !$tag instanceof AsTagged)
62
            ->tap(static fn (mixed $tag) => throw AttributeException::mustBeOfType(AsTagged::class, $tag))
63
        ;
64
    }
65
}
66