Test Failed
Push — master ( 741545...e25e21 )
by Julien
11:23
created

Task::normalizeResponse()   C

Complexity

Conditions 12
Paths 64

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 53
ccs 0
cts 27
cp 0
rs 6.9666
cc 12
nc 64
nop 5
crap 156

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Modules\Cli;
12
13
use Phalcon\Cli\Dispatcher;
14
use Phalcon\Version;
15
use Zemit\Utils;
16
17
/**
18
 * Class Task
19
 *
20
 * @author Julien Turbide <[email protected]>
21
 * @copyright Zemit Team <[email protected]>
22
 *
23
 * @since 1.0
24
 * @version 1.0
25
 *
26
 * @package Zemit\Modules\Cli
27
 */
28
class Task extends \Phalcon\Cli\Task
29
{
30
    /**
31
     * @var string
32
     */
33
    public $consoleDoc = <<<DOC
34
Usage:
35
  php zemit cli <task> <action> [<params> ...]
36
37
Options:
38
  task: build,cache,cron,errors,help,scaffold
39
40
41
DOC;
42
    
43
    public function helpAction()
44
    {
45
        echo $this->consoleDoc;
46
    }
47
    
48
    public function mainAction()
49
    {
50
        $this->helpAction();
51
    }
52
    
53
    /**
54
     * Sending rest response as an http response
55
     *
56
     * @param array|null $response
57
     * @param null $status
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $status is correct as it would always require null to be passed?
Loading history...
58
     * @param null $code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $code is correct as it would always require null to be passed?
Loading history...
59
     * @param int $jsonOptions
60
     * @param int $depth
61
     *
62
     * @return array
63
     */
64
    public function normalizeResponse($response = null, $code = null, $status = null, $jsonOptions = 0, $depth = 512)
0 ignored issues
show
Unused Code introduced by
The parameter $jsonOptions is not used and could be removed. ( Ignorable by Annotation )

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

64
    public function normalizeResponse($response = null, $code = null, $status = null, /** @scrutinizer ignore-unused */ $jsonOptions = 0, $depth = 512)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $depth is not used and could be removed. ( Ignorable by Annotation )

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

64
    public function normalizeResponse($response = null, $code = null, $status = null, $jsonOptions = 0, /** @scrutinizer ignore-unused */ $depth = 512)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        $debug = $this->config->app->debug ?: $this->dispatcher->getParam('debug') ?: false;
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist on Zemit\Modules\Cli\Task. Since you implemented __get, consider adding a @property annotation.
Loading history...
67
        
68
        // keep forced status code or set our own
69
        $responseStatusCode = $this->response->getStatusCode();
70
        $reasonPhrase = $this->response->getReasonPhrase();
71
        $status ??= $reasonPhrase ? : 'OK';
72
        $code ??= (int)$responseStatusCode ? : 200;
73
        $view = $this->view->getParamsToView();
74
        $hash = hash('sha512', json_encode($view));
75
        
76
        /**
77
         * Debug section
78
         * - Versions
79
         * - Request
80
         * - Identity
81
         * - Profiler
82
         * - Dispatcher
83
         * - Router
84
         */
85
        $identity = $debug ? $this->identity->getIdentity() : null;
0 ignored issues
show
Bug Best Practice introduced by
The property identity does not exist on Zemit\Modules\Cli\Task. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getIdentity() does not exist on null. ( Ignorable by Annotation )

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

85
        $identity = $debug ? $this->identity->/** @scrutinizer ignore-call */ getIdentity() : null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        $profiler = $debug && $this->profiler ? $this->profiler->toArray() : null;
0 ignored issues
show
Bug Best Practice introduced by
The property profiler does not exist on Zemit\Modules\Cli\Task. Since you implemented __get, consider adding a @property annotation.
Loading history...
87
        $dispatcher = $debug ? $this->dispatcher->toArray() : null;
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Phalcon\Mvc\DispatcherInterface. It seems like you code against a sub-type of Phalcon\Mvc\DispatcherInterface such as Zemit\Mvc\Dispatcher. ( Ignorable by Annotation )

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

87
        $dispatcher = $debug ? $this->dispatcher->/** @scrutinizer ignore-call */ toArray() : null;
Loading history...
introduced by
The method toArray() does not exist on Phalcon\Mvc\Dispatcher. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

87
        $dispatcher = $debug ? $this->dispatcher->/** @scrutinizer ignore-call */ toArray() : null;
Loading history...
88
        $router = $debug ? $this->router->toArray() : null;
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on Phalcon\Mvc\Router. It seems like you code against a sub-type of Phalcon\Mvc\Router such as Zemit\Mvc\Router. ( Ignorable by Annotation )

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

88
        $router = $debug ? $this->router->/** @scrutinizer ignore-call */ toArray() : null;
Loading history...
Bug introduced by
The method toArray() does not exist on Phalcon\Mvc\RouterInterface. It seems like you code against a sub-type of Phalcon\Mvc\RouterInterface such as Zemit\Mvc\Router. ( Ignorable by Annotation )

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

88
        $router = $debug ? $this->router->/** @scrutinizer ignore-call */ toArray() : null;
Loading history...
89
        
90
        $api = $debug ? [
91
            'php' => phpversion(),
92
            'phalcon' => Version::get(),
93
            'zemit' => $this->config->core->version,
94
            'core' => $this->config->core->name,
95
            'app' => $this->config->app->version,
96
            'name' => $this->config->app->name,
97
        ] : [];
98
        $api['version'] = '0.1';
99
        
100
        $this->response->setStatusCode($code, $code . ' ' . $status);
101
        
102
        return array_merge([
103
            'api' => $api,
104
            'timestamp' => date('c'),
105
            'hash' => $hash,
106
            'status' => $status,
107
            'code' => $code,
108
            'response' => $response,
109
            'view' => $view,
110
        ], $debug ? [
111
            'identity' => $identity,
112
            'profiler' => $profiler,
113
            'dispatcher' => $dispatcher,
114
            'router' => $router,
115
            'memory' => Utils::getMemoryUsage(),
116
        ] : []);
117
    }
118
    
119
    /**
120
     * Handle rest response automagically
121
     *
122
     * @param Dispatcher $dispatcher
123
     */
124
    public function afterExecuteRoute(Dispatcher $dispatcher)
125
    {
126
        // Merge response into view variables
127
        $response = $dispatcher->getReturnedValue();
128
    
129
        // Quiet output
130
        $quiet = $this->dispatcher->getParam('quiet');
131
        if ($quiet) {
132
            exit(!$response? 1 : 0);
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
133
        }
134
    
135
        // Normalize response
136
        if (is_array($response)) {
137
            $this->view->setVars($response, true);
0 ignored issues
show
Bug introduced by
The method setVars() does not exist on Phalcon\Mvc\ViewInterface. Did you maybe mean setVar()? ( Ignorable by Annotation )

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

137
            $this->view->/** @scrutinizer ignore-call */ 
138
                         setVars($response, true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
        }
139
        $normalizedResponse = $this->normalizeResponse(is_array($response) ? null : $response);
140
        $dispatcher->setReturnedValue($normalizedResponse);
141
    
142
        // Set response
143
        $verbose = $this->dispatcher->getParam('verbose');
144
        $ret = $verbose? $normalizedResponse : $response;
145
        
146
        // Format response
147
        $format = $this->dispatcher->getParam('format');
148
        $format ??= 'dump';
149
        
150
        switch (strtolower($format)) {
151
            case 'dump':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
152
                dd($ret);
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
153
            case 'json':
154
                echo json_encode($ret);
155
                break;
156
            case 'xml':
157
                $xml = new \SimpleXMLElement('');
158
                array_walk_recursive($ret, array ($xml,'addChild'));
159
                echo $xml->asXML();
0 ignored issues
show
Bug introduced by
Are you sure $xml->asXML() of type string|true can be used in echo? ( Ignorable by Annotation )

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

159
                echo /** @scrutinizer ignore-type */ $xml->asXML();
Loading history...
160
                break;
161
            case 'serialize':
162
                echo serialize($ret);
163
                break;
164
            case 'raw':
165
                if (is_array($ret) || is_object($ret)) {
166
                    print_r($ret);
167
                }
168
                else if (is_bool($ret)) {
169
                    echo $ret? 'true' : 'false';
170
                }
171
                else {
172
                    echo strval($ret);
173
                }
174
                break;
175
            default:
176
                throw new \Exception('Unknown output format `'.$format.'` expected one of the string value: `json` `xml` `serialize` `dump` `raw`');
177
        }
178
    }
179
}
180