Passed
Push — main ( 5fbd1e...a5917e )
by Thierry
10:54
created

Call::_makeUniqueJsVar()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 21
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Call.php - The Jaxon Call
5
 *
6
 * This class is used to create js ajax requests to callable classes and functions.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
10
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
11
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
12
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-core
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
20
21
namespace Jaxon\Request\Call;
22
23
use Jaxon\App\Dialog\Library\DialogLibraryManager;
24
use Jaxon\Plugin\Response\JQuery\DomSelector;
25
26
use function array_shift;
27
use function implode;
28
29
class Call extends JsCall
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Call
Loading history...
30
{
31
    use Traits\CallConditionTrait;
32
    use Traits\CallMessageTrait;
33
34
    /**
35
     * @var DialogLibraryManager
36
     */
37
    protected $xDialogLibraryManager;
38
39
    /**
40
     * @var Paginator
41
     */
42
    protected $xPaginator;
43
44
    /**
45
     * @var array
46
     */
47
    private $aVariables;
0 ignored issues
show
introduced by
The private property $aVariables is not used, and could be removed.
Loading history...
48
49
    /**
50
     * @var string
51
     */
52
    private $sVars;
53
54
    /**
55
     * @var int
0 ignored issues
show
Bug introduced by
Expected "integer" but found "int" for @var tag in member variable comment
Loading history...
56
     */
57
    private $nVarId;
0 ignored issues
show
introduced by
The private property $nVarId is not used, and could be removed.
Loading history...
58
59
    /**
60
     * The constructor.
61
     *
62
     * @param string $sName    The javascript function or method name
0 ignored issues
show
Coding Style introduced by
Expected 15 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 17 spaces after parameter name; 4 found
Loading history...
63
     * @param DialogLibraryManager $xDialogLibraryManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
64
     * @param Paginator $xPaginator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 12 spaces after parameter type; 1 found
Loading history...
65
     */
66
    public function __construct(string $sName, DialogLibraryManager $xDialogLibraryManager, Paginator $xPaginator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
67
    {
68
        parent::__construct($sName);
69
        $this->xDialogLibraryManager = $xDialogLibraryManager;
70
        $this->xPaginator = $xPaginator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
71
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
72
73
    /**
74
     * Make a phrase to be displayed in js code
75
     *
76
     * @param array $aArgs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
77
     *
78
     * @return string
79
     */
80
    private function makePhrase(array $aArgs): string
81
    {
82
        if(empty($aArgs))
83
        {
84
            return '';
85
        }
86
        // The first array entry is the message.
87
        $sPhrase = array_shift($aArgs);
88
        if(empty($aArgs))
89
        {
90
            return $sPhrase;
91
        }
92
        $nParamId = 1;
93
        foreach($aArgs as &$xParameter)
94
        {
95
            $xParameter = "'$nParamId':" . $xParameter->getScript();
96
            $nParamId++;
97
        }
98
        return $sPhrase . '.supplant({' . implode(',', $aArgs) . '})';
99
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
100
101
    /**
102
     * Make a phrase to be displayed in js code
103
     *
104
     * @return string
105
     */
106
    private function makeMessage(): string
107
    {
108
        if(!($sPhrase = $this->makePhrase($this->aMessageArgs)))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
109
        {
110
            return '';
111
        }
112
        $sMethod = $this->sMessageType;
113
        $xLibrary = $this->xDialogLibraryManager->getMessageLibrary();
114
        $xLibrary->setReturnCode(true);
115
        return $xLibrary->$sMethod($sPhrase);
116
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
117
118
    /**
119
     * Returns a string representation of the script output (javascript) from this request object
120
     *
121
     * @return string
122
     */
123
    public function getScript(): string
124
    {
125
        $sMessageScript = $this->makeMessage();
126
        $sScript = parent::getScript();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
127
        if($this->bConfirm)
128
        {
129
            $sConfirmPhrase = $this->makePhrase($this->aConfirmArgs);
130
            $sScript = $this->xDialogLibraryManager->getQuestionLibrary()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
131
                ->confirm($sConfirmPhrase, $sScript, $sMessageScript);
0 ignored issues
show
Coding Style introduced by
Space found before object operator
Loading history...
132
        }
133
        if($this->sCondition !== '')
134
        {
135
            $sScript = empty($sMessageScript) ? 'if(' . $this->sCondition . '){' . $sScript . ';}' :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
136
                'if(' . $this->sCondition . '){' . $sScript . ';}else{' . $sMessageScript . ';}';
137
        }
138
        return $this->sVars . $sScript;
139
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
140
141
    /**
142
     * Check if the request has a parameter of type Parameter::PAGE_NUMBER
143
     *
144
     * @return bool
145
     */
146
    public function hasPageNumber(): bool
147
    {
148
        foreach($this->aParameters as $xParameter)
149
        {
150
            if($xParameter->getType() === Parameter::PAGE_NUMBER)
151
            {
152
                return true;
153
            }
154
        }
155
        return false;
156
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
157
158
    /**
159
     * Set a value to the Parameter::PAGE_NUMBER parameter
160
     *
161
     * @param integer $nPageNumber    The current page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
162
     *
163
     * @return Call
164
     */
165
    public function setPageNumber(int $nPageNumber): Call
166
    {
167
        // Set the value of the Parameter::PAGE_NUMBER parameter
168
        foreach($this->aParameters as $xParameter)
169
        {
170
            if($xParameter->getType() === Parameter::PAGE_NUMBER)
171
            {
172
                $xParameter->setValue($nPageNumber);
173
                break;
174
            }
175
        }
176
        return $this;
177
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
178
179
    /**
180
     * Make the pagination links for this request
181
     *
182
     * @param integer $nCurrentPage    The current page
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
183
     * @param integer $nItemsPerPage    The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
184
     * @param integer $nItemsTotal    The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
185
     *
186
     * @return Paginator
187
     */
188
    public function pg(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator
189
    {
190
        // Append the page number to the parameter list, if not yet given.
191
        if(!$this->hasPageNumber())
192
        {
193
            $this->addParameter(Parameter::PAGE_NUMBER, 0);
194
        }
195
        return $this->xPaginator->setup($this, $nCurrentPage, $nItemsPerPage, $nItemsTotal);
196
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
197
198
    /**
199
     * Make the pagination links for this request
200
     *
201
     * @param integer $nCurrentPage    The current page
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
202
     * @param integer $nItemsPerPage    The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
203
     * @param integer $nItemsTotal    The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
204
     *
205
     * @return Paginator
206
     */
207
    public function paginate(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator
208
    {
209
        return $this->pg($nCurrentPage, $nItemsPerPage, $nItemsTotal);
210
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
211
212
    /**
213
     * Make the pagination links for this request
214
     *
215
     * @param integer $nCurrentPage    The current page
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
216
     * @param integer $nItemsPerPage    The number of items per page
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
217
     * @param integer $nItemsTotal    The total number of items
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
218
     *
219
     * @return array
220
     */
221
    public function pages(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): array
222
    {
223
        return $this->pg($nCurrentPage, $nItemsPerPage, $nItemsTotal)->pages();
224
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
225
}
226