Passed
Push — master ( 1d30f9...202808 )
by Peter
04:48
created

ApiClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 25
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 10

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
namespace AbterPhp\Admin\Http\Controllers\Admin\Form;
6
7
use AbterPhp\Admin\Domain\Entities\ApiClient as Entity;
8
use AbterPhp\Admin\Form\Factory\ApiClient as FormFactory;
9
use AbterPhp\Admin\Orm\ApiClientRepo as Repo;
10
use AbterPhp\Framework\Assets\AssetManager;
11
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
12
use AbterPhp\Framework\Http\Controllers\Admin\FormAbstract;
13
use AbterPhp\Framework\I18n\ITranslator;
14
use AbterPhp\Framework\Session\FlashService;
15
use Cocur\Slugify\Slugify;
16
use Opulence\Events\Dispatchers\IEventDispatcher;
17
use Opulence\Routing\Urls\UrlGenerator;
18
use Opulence\Sessions\ISession;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
23
 */
24
class ApiClient extends FormAbstract
25
{
26
    const ENTITY_SINGULAR = 'apiClient';
27
    const ENTITY_PLURAL   = 'apiClients';
28
29
    const ENTITY_TITLE_SINGULAR = 'admin:apiClient';
30
    const ENTITY_TITLE_PLURAL   = 'admin:apiClients';
31
32
    /** @var Slugify */
33
    protected $slugify;
34
35
    /** @var AssetManager */
36
    protected $assets;
37
38
    /** @var string */
39
    protected $secretLength;
40
41
    /** @var string */
42
    protected $resource = 'api_clients';
43
44
    /**
45
     * ApiClient constructor.
46
     *
47
     * @param FlashService     $flashService
48
     * @param ITranslator      $translator
49
     * @param UrlGenerator     $urlGenerator
50
     * @param LoggerInterface  $logger
51
     * @param Repo             $repo
52
     * @param ISession         $session
53
     * @param FormFactory      $formFactory
54
     * @param IEventDispatcher $eventDispatcher
55
     * @param AssetManager     $assetManager
56
     * @param string           $secretLength
57
     */
58
    public function __construct(
59
        FlashService $flashService,
60
        ITranslator $translator,
61
        UrlGenerator $urlGenerator,
62
        LoggerInterface $logger,
63
        Repo $repo,
64
        ISession $session,
65
        FormFactory $formFactory,
66
        IEventDispatcher $eventDispatcher,
67
        AssetManager $assetManager,
68
        string $secretLength
69
    ) {
70
        parent::__construct(
71
            $flashService,
72
            $translator,
73
            $urlGenerator,
74
            $logger,
0 ignored issues
show
Bug introduced by
$logger of type Psr\Log\LoggerInterface is incompatible with the type AbterPhp\Framework\Orm\IGridRepo expected by parameter $repo of AbterPhp\Framework\Http\...Abstract::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            /** @scrutinizer ignore-type */ $logger,
Loading history...
75
            $repo,
0 ignored issues
show
Bug introduced by
$repo of type AbterPhp\Admin\Orm\ApiClientRepo is incompatible with the type Opulence\Sessions\ISession expected by parameter $session of AbterPhp\Framework\Http\...Abstract::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            /** @scrutinizer ignore-type */ $repo,
Loading history...
76
            $session,
0 ignored issues
show
Bug introduced by
$session of type Opulence\Sessions\ISession is incompatible with the type AbterPhp\Framework\Form\Factory\IFormFactory expected by parameter $formFactory of AbterPhp\Framework\Http\...Abstract::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            /** @scrutinizer ignore-type */ $session,
Loading history...
77
            $formFactory,
0 ignored issues
show
Bug introduced by
$formFactory of type AbterPhp\Admin\Form\Factory\ApiClient is incompatible with the type Opulence\Events\Dispatchers\IEventDispatcher expected by parameter $eventDispatcher of AbterPhp\Framework\Http\...Abstract::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            /** @scrutinizer ignore-type */ $formFactory,
Loading history...
78
            $eventDispatcher
0 ignored issues
show
Unused Code introduced by
The call to AbterPhp\Framework\Http\...Abstract::__construct() has too many arguments starting with $eventDispatcher. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        parent::/** @scrutinizer ignore-call */ 
79
                __construct(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
79
        );
80
81
        $this->assets       = $assetManager;
82
        $this->secretLength = $secretLength;
83
    }
84
85
    /**
86
     * @param string $entityId
87
     *
88
     * @return Entity
89
     */
90
    public function createEntity(string $entityId): IStringerEntity
91
    {
92
        $entity = new Entity((string)$entityId, '', '', '');
93
94
        return $entity;
95
    }
96
97
    /**
98
     * @param Entity|null $entity
99
     *
100
     * @throws OrmException
101
     */
102
    protected function addCustomAssets(?IStringerEntity $entity = null)
103
    {
104
        parent::addCustomAssets($entity);
105
106
        $footerResource = $this->getResourceName(static::RESOURCE_FOOTER);
107
108
        $this->assets->addJs(
109
            $footerResource,
110
            '/admin-assets/vendor/password-generator/password-generator.js'
111
        );
112
        $this->assets->addJsContent(
113
            $footerResource,
114
            "var secretLength = '{$this->secretLength}'"
115
        );
116
        $this->assets->addJs(
117
            $footerResource,
118
            '/admin-assets/js/api-client.js'
119
        );
120
    }
121
}
122