Completed
Push — master ( 6e89de...023d00 )
by Mihail
02:41
created

Controller::make()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 42
rs 8.439
cc 6
eloc 24
nc 10
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Controller::after() 0 1 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
     * Build variables and display output html
37
     */
38
    public function getOutput()
39
    {
40
        $this->after();
41
42
        // if layout is not required and this is just standalone app
43
        if ($this->layout === null) {
44
            $content = $this->response;
45
        } else {
46
            $layoutPath = App::$Alias->currentViewPath . '/layout/' . $this->layout . '.php';
47
            if (!File::exist($layoutPath)) {
48
                throw new NativeException('Layout not founded: ' . $layoutPath);
49
            }
50
51
            $body = $this->response;
52
            // pass global data to config viewer
53
            if (App::$Debug !== null) {
54
                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...
55
            }
56
57
            // cleanup buffer from random shits after exception throw'd
58
            ob_clean();
59
            // start buffering to render layout
60
            ob_start();
61
            include($layoutPath);
62
            $content = ob_get_clean(); // read buffer content & stop buffering
63
64
            // set custom css library's not included on static call
65
            $cssIncludeCode = App::$View->showCodeLink('css');
66
            if (!Str::likeEmpty($cssIncludeCode)) {
0 ignored issues
show
Bug introduced by
It seems like $cssIncludeCode defined by \Ffcms\Core\App::$View->showCodeLink('css') on line 65 can also be of type string; however, Ffcms\Core\Helper\Type\Str::likeEmpty() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
67
                $content = Str::replace('</head>', $cssIncludeCode . '</head>', $content);
68
            }
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
        return $content;
81
    }
82
83
    public function after() {}
84
85
    /**
86
     * Set single global variable
87
     * @param string $var
88
     * @param string $value
89
     * @param bool $html
90
     */
91
    public function setGlobalVar($var, $value, $html = false)
92
    {
93
        Variables::instance()->setGlobal($var, $value, $html);
94
    }
95
96
    /**
97
     * Set global variables as array key=>value
98
     * @param $array
99
     */
100
    public function setGlobalVarArray(array $array)
101
    {
102
        Variables::instance()->setGlobalArray($array);
103
    }
104
105
    /**
106
     * Special method to set response of action execution
107
     * @param string $response
108
     */
109
    public function setResponse($response)
110
    {
111
        $this->response = $response;
112
    }
113
114
}