Completed
Push — master ( f4fae9...d97ad7 )
by Mihail
09:18
created

Controller::setResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ffcms\Core\Arch;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Exception\NativeException;
7
use Ffcms\Core\Helper\FileSystem\File;
8
use Ffcms\Core\Helper\Type\Str;
9
use Ffcms\Core\Traits\DynamicGlobal;
10
use Ffcms\Core\Template\Variables;
11
12
class Controller
13
{
14
15
    use DynamicGlobal;
16
17
    /**
18
     * @var string $layout
19
     */
20
    public $layout = 'main';
21
22
    /**
23
     * @var string $response
24
     */
25
    public $response;
26
27
28
    public function __construct()
29
    {
30
        $this->before();
31
    }
32
33
    public function before() {}
34
35
    /**
36
     * Compile output
37
     */
38
    public function __destruct()
39
    {
40
        // allow use and override after() method
41
        $this->after();
42
        $this->make();
43
    }
44
45
    /**
46
     * Build variables and display output html
47
     */
48
    protected function make()
49
    {
50
        // if layout is not required and this is just standalone app
51
        if ($this->layout === null) {
52
            $content = $this->response;
53
        } else {
54
            $layoutPath = App::$Alias->currentViewPath . '/layout/' . $this->layout . '.php';
55
            if (!File::exist($layoutPath)) {
56
                throw new NativeException('Layout not founded: {root}' . Str::replace(root, '', $layoutPath));
57
            }
58
59
            $body = $this->response;
0 ignored issues
show
Unused Code introduced by
$body is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
            // pass global data to config viewer
61
            if (App::$Debug !== null) {
62
                App::$Debug->bar->getCollector('config')->setData(['Global Vars' => Variables::instance()->getGlobalsArray()]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface DebugBar\DataCollector\DataCollectorInterface as the method setData() does only exist in the following implementations of said interface: DebugBar\DataCollector\ConfigCollector.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
63
            }
64
65
            ob_start();
66
            include_once($layoutPath);
67
            $content = ob_get_contents();
68
            ob_end_clean();
69
70
            // add debug bar
71
            if (App::$Debug !== null) {
72
                $content = str_replace(
73
                    ['</body>', '</head>'],
74
                    [App::$Debug->renderOut() . '</body>', App::$Debug->renderHead() . '</head>'],
75
                    $content);
76
            }
77
78
        }
79
80
        // display content and layout if exist
81
        App::$Response->setContent($content);
82
        App::$Response->send();
83
    }
84
85
    public function after() {}
86
87
    /**
88
     * Set single global variable
89
     * @param string $var
90
     * @param string $value
91
     * @param bool $html
92
     */
93
    public function setGlobalVar($var, $value, $html = false)
94
    {
95
        Variables::instance()->setGlobal($var, $value, $html);
96
    }
97
98
    /**
99
     * Set global variables as array key=>value
100
     * @param $array
101
     */
102
    public function setGlobalVarArray(array $array)
103
    {
104
        Variables::instance()->setGlobalArray($array);
105
    }
106
107
    /**
108
     * Special method to set response of action execution
109
     * @param string $response
110
     */
111
    public function setResponse($response)
112
    {
113
        $this->response = $response;
114
    }
115
116
}