CallableClassHelper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 5
rs 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
/**
4
 * CallableClassHelper.php
5
 *
6
 * Provides properties to a callable class.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Plugin\Request\CallableClass;
16
17
use Jaxon\App\Session\SessionInterface;
18
use Jaxon\App\Stash\Stash;
19
use Jaxon\App\View\ViewRenderer;
20
use Jaxon\Di\ClassContainer;
21
use Jaxon\Exception\SetupException;
22
use Jaxon\Script\Factory\CallFactory;
23
use Jaxon\Script\JxnCall;
24
use Jaxon\Request\Target;
25
use Jaxon\Request\Upload\UploadHandlerInterface;
26
use Psr\Log\LoggerInterface;
27
28
use function trim;
29
30
class CallableClassHelper
31
{
32
    /**
33
     * @var Target
34
     */
35
    public $xTarget;
36
37
    /**
38
     * The constructor
39
     *
40
     * @param ClassContainer $cls
41
     * @param JxnCall $xJxnCall
42
     * @param CallFactory $xFactory
43
     * @param ViewRenderer $xViewRenderer
44
     * @param LoggerInterface $xLogger
45
     * @param SessionInterface $xSessionManager
46
     * @param UploadHandlerInterface|null $xUploadHandler
47
     *
48
     * @throws SetupException
49
     */
50
    public function __construct(public ClassContainer $cls, public JxnCall $xJxnCall,
51
        public CallFactory $xFactory, public ViewRenderer $xViewRenderer,
52
        public LoggerInterface $xLogger, public ?SessionInterface $xSessionManager,
53
        public Stash $xStash, public ?UploadHandlerInterface $xUploadHandler)
54
    {}
55
56
    /**
57
     * Get an instance of a Jaxon class by name
58
     *
59
     * @param string $sClassName the class name
60
     *
61
     * @return mixed
62
     * @throws SetupException
63
     */
64
    public function cl(string $sClassName)
65
    {
66
        return $this->cls->makeRegisteredObject($sClassName);
67
    }
68
69
    /**
70
     * Get the js call factory.
71
     *
72
     * @param string $sClassName
73
     *
74
     * @return JxnCall
75
     */
76
    public function rq(string $sClassName = ''): JxnCall
77
    {
78
        $sClassName = trim($sClassName);
79
        return !$sClassName ? $this->xJxnCall : $this->xFactory->rq($sClassName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! $sClassName ? $...actory->rq($sClassName) could return the type null which is incompatible with the type-hinted return Jaxon\Script\JxnCall. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
}
82