Completed
Pull Request — master (#81)
by
unknown
03:05
created

App::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Zewa;
4
5
//use Zewa\Interfaces\ContainerInterface;
6
7
/**
8
 * This class is the starting point for application
9
 *
10
 * @author Zechariah Walden<zech @ zewadesign.com>
11
 */
12
class App
13
{
14
    /**
15
     * Events
16
     */
17
    private static $events;
18
19
    /**
20
     * Return value from application
21
     *
22
     * @var string
23
     */
24
    private $output = false;
25
26
    /**
27
     * Namespaced controller path
28
     *
29
     * @var string
30
     */
31
    private $class;
32
33
    /**
34
     * Instantiated class object
35
     *
36
     * @var Controller
37
     */
38
    private $instantiatedClass;
39
40
    /**
41
     * Module being accessed
42
     *
43
     * @var string
44
     */
45
    private $module;
46
47
    /**
48
     * Controller being accessed
49
     *
50
     * @var string
51
     */
52
    private $controller;
53
54
    /**
55
     * Method being accessed
56
     *
57
     * @var string
58
     */
59
    private $method;
60
61
    /**
62
     * Params being passed
63
     *
64
     * @var array
65
     */
66
    private $params;
67
68
    /**
69
     * @var DIContainer $container
70
     */
71
    private $container;
72
73
    /**
74
     * Application bootstrap process
75
     *
76
     * The application registers the configuration in the app/config/core.php
77
     * and then processes, and makes available the configured resources
78
     *
79
     * App constructor.
80
     * @param Config $config
81
     * @param DIContainer $container
82
     */
83
    public function __construct(Config $config, DIContainer $container)
84
    {
85
        $this->configuration = $config;
0 ignored issues
show
Bug introduced by
The property configuration does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
86
        $this->container = $container;
87
88
        $this->router = $container->resolve('\Zewa\Router', true);
0 ignored issues
show
Bug introduced by
The property router does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
89
        $this->request = $container->resolve('\Zewa\Request', true);
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
90
        $this->view = $container->resolve('\Zewa\View');
0 ignored issues
show
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
91
92
        $routerConfig = $this->router->getConfig();
93
        $config->set('Routing', $routerConfig->get('Routing'));
94
95
        $this->prepare();
96
    }
97
98
    /**
99
     * Calls the proper shell for app execution
100
     *
101
     * @access private
102
     */
103
    public function initialize()
104
    {
105
        $this->start();
106
        return $this;
107
    }
108
109
    /**
110
     * App preparation cycle
111
     */
112
    private function prepare()
113
    {
114
        $routerConfig = $this->configuration->get('Routing');
115
116
        $this->module = ucfirst($routerConfig->module);
117
        $this->controller = ucfirst($routerConfig->controller);
118
        $this->method = $routerConfig->method;
119
        $this->params = $routerConfig->params;
120
        $this->class = 'Zewa\\App\\Modules\\' . $this->module . '\\Controllers\\' . ucfirst($this->controller);
121
    }
122
123 28
//    public function setContainer(Container $container)
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
124
//    {
125 28
//        $this->container = $container;
126
//    }
127
128
    /**
129
     * Verifies the provided application request is a valid request
130 28
     *
131 28
     * @access private
132
     */
133 28
    private function validateRequest()
134 28
    {
135 28
        //catch exception and handle
136 28
        try {
137 28
            $class = new \ReflectionClass($this->class);
138 28
            $class->getMethod($this->method);
139
        } catch (\ReflectionException $e) {
140
            $view = $this->container->resolve('\Zewa\View');
141 28
            $this->output = $view->render404(['Invalid method requests']); //Router::show404(
142 28
            return false;
143
        }
144 28
145 18
        return true;
146
    }
147
148
    /**
149
     * Processes the application request
150
     *
151
     * @access private
152
     */
153
    private function start()
154
    {
155
        if ($this->validateRequest() === false) {
156
            return false;
157
        }
158
159
        App::callEvent('preController');
160
        $this->instantiatedClass = $this->container->resolve($this->class);
161 28
        App::callEvent('postController');
162
163 28
        $this->instantiatedClass->setConfig($this->configuration);
164
        $this->instantiatedClass->setRouter($this->router);
165 28
        $this->instantiatedClass->setRequest($this->request);
166
        $this->instantiatedClass->setContainer($this->container);
167 28
        $this->instantiatedClass->setView($this->view);
168 18
169 18
        $this->output = call_user_func_array(
170
            [&$this->instantiatedClass, $this->method],
171 18
            $this->params
172 18
        );
173 18
    }
174
175 18
    /**
176 18
     * Attach (or remove) multiple callbacks to an event and trigger those callbacks when that event is called.
177 18
     *
178 18
     * @param string $event    name
179 18
     * @param mixed  $value    the optional value to pass to each callback
180 18
     * @param mixed  $callback the method or function to call - FALSE to remove all callbacks for event
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
181
     */
182 18
183
    public static function addEvent($event, $callback = false)
184 18
    {
185
        // Adding or removing a callback?
186
        if ($callback !== false) {
187
            self::$events[$event][] = $callback;
188
        } else {
189
            unset(self::$events[$event]);
190
        }
191 18
    }
192 18
193
    public function callEvent($event, $method = false, $arguments = [])
194 18
    {
195
        if (isset(self::$events[$event])) {
196 3
            foreach (self::$events[$event] as $e) {
197
                if ($method !== false) { // class w/ method specified
198 3
                    $object = new $e();
199
                    $value = call_user_func_array(
200
                        [&$object, $method],
201
                        $arguments
202 3
                    );
203 3
                } else {
204
                    if (class_exists($e)) {
205
                        $value = new $e($arguments); // class w/o method specified
206
                    } else {
207
                        $value = call_user_func($e, $arguments); // function yuk
208
                    }
209
                }
210 18
            }
211
212 18
            return $value;
0 ignored issues
show
Bug introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
213 18
        }
214
    }
215
216 18
217 18
    /**
218
     * Prepare application return value into a string
219
     *
220
     * @access public
221
     * @return string
222
     */
223 28
    public function __toString()
224
    {
225 28
        if (!$this->output) {
226 28
            $this->output = '';
227 15
        }
228 28
229 28
        App::callEvent('postApplication');
230
231 28
        return $this->output;
232
    }
233
}
234