Passed
Push — master ( 5551ce...816e3b )
by Oleg
04:15
created

Micro::getWebDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php /** Micro */
2
3
namespace Micro;
4
5
use Micro\base\Container;
6
use Micro\base\Dispatcher;
7
use Micro\Base\Exception;
8
use Micro\Base\FatalError;
9
use Micro\Base\ICommand;
10
use Micro\base\IContainer;
11
use Micro\cli\Consoles\DefaultConsoleCommand;
12
use Micro\Mvc\Controllers\IController;
13
use Micro\Resolver\IResolver;
14
use Micro\web\IOutput;
15
use Micro\web\IRequest;
16
use Micro\Web\IResponse;
17
use Micro\web\Response;
18
19
/**
20
 * Micro class file.
21
 *
22
 * Base class for initialize MicroPHP, used as bootstrap framework.
23
 *
24
 * @author Oleg Lunegov <[email protected]>
25
 * @link https://github.com/lugnsk/micro
26
 * @copyright Copyright &copy; 2013 Oleg Lunegov
27
 * @license /LICENSE
28
 * @package micro
29
 * @version 1.0
30
 * @since 1.0
31
 */
32
class Micro
33
{
34
    /** @const string VERSION Version framework */
35
    const VERSION = '1.1';
36
37
    /** @var IContainer $container Container is a container for components and options */
38
    protected $container;
39
    /** @var string $appDir */
40
    protected $appDir;
41
    /** @var string $webDir */
42
    protected $webDir;
43
44
    /** @var bool $loaded Micro loaded flag */
45
    private $loaded;
46
    /** @var bool $debug Debug-mode flag */
47
    private $debug = true;
48
    /** @var string $environment Application environment */
49
    private $environment = 'devel';
50
    /** @var float $startTime Time of start framework */
51
    private $startTime;
52
53
54
    /**
55
     * Initialize application
56
     *
57
     * @access public
58
     *
59
     * @param string $environment Application environment: devel , production , test, other
60
     * @param bool $debug Debug-mode flag
61
     *
62
     * @result void
63
     */
64
    public function __construct($environment = 'devel', $debug = true)
65
    {
66
        $this->webDir = getenv('DOCUMENT_ROOT');
67
        $this->environment = (string)$environment;
68
        $this->debug = (bool)$debug;
69
        $this->loaded = false;
70
71
        ini_set('display_errors', (integer)$this->debug);
72
        ini_set('log_errors', (integer)$this->debug);
73
74
        FatalError::register();
75
76
        if ($this->debug) {
77
            ini_set('error_reporting', -1);
78
            $this->startTime = microtime(true);
79
        }
80
    }
81
82
    /**
83
     * Clone application
84
     *
85
     * @access public
86
     *
87
     * @return void
88
     */
89
    public function __clone()
90
    {
91
        if ($this->debug) { // start new timer
92
            $this->startTime = microtime(true);
93
        }
94
95
        $this->loaded = false; // deactivate loaded
96
        $this->container = null; // remove configured container
97
    }
98
99
    /**
100
     * Running application
101
     *
102
     * @access public
103
     *
104
     * @param IRequest $request Request object
105
     *
106
     * @return Response
107
     * @throws \Exception
108
     */
109
    public function run(IRequest $request)
110
    {
111
        try {
112
            return $this->doRun($request);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doRun($request); of type Micro\Web\IResponse|string adds the type string to the return on line 112 which is incompatible with the return type documented by Micro\Micro::run of type Micro\Web\Response.
Loading history...
113
        } catch (\Exception $e) {
114
            if ($this->debug) {
115
                $this->container->dispatcher->signal('kernel.exception', ['exception' => $e]);
0 ignored issues
show
Bug introduced by
Accessing dispatcher on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
116
                throw $e;
117
            }
118
119
            return $this->doException($e);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doException($e); of type Micro\Cli\Consoles\Defau...mand|Micro\Web\Response adds the type Micro\Cli\Consoles\DefaultConsoleCommand to the return on line 119 which is incompatible with the return type documented by Micro\Micro::run of type Micro\Web\Response.
Loading history...
120
        }
121
    }
122
123
    /**
124
     * Starting ...
125
     *
126
     * @access private
127
     *
128
     * @param IRequest $request
129
     *
130
     * @return Web\IResponse|Response|string
131
     * @throws \Micro\Base\Exception
132
     */
133
    private function doRun(IRequest $request)
134
    {
135
        if (!$this->loaded) {
136
            $this->initializeContainer();
137
138
            $this->addListener('kernel.kill', function(array $params) {
139
                if ($params['container']->kernel->isDebug() && !$params['container']->request->isCli()) {
140
                    // Add timer into page
141
                    echo '<div class=debug_timer>', (microtime(true) - $params['container']->kernel->getStartTime()), '</div>';
142
                }
143
144
                if (false === $params['container']->kernel->loaded) {
145
                    return;
146
                }
147
148
                $params['container']->kernel->container = null;
149
                $params['container']->kernel->loaded = false;
150
            });
151
        }
152
153
        $this->container->request = $request;
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
154
        if ($output = $this->sendSignal('kernel.request', ['container' => $this->container]) instanceof IResponse) {
155
            return $output;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $output; (boolean) is incompatible with the return type documented by Micro\Micro::doRun of type Micro\Web\IResponse|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
156
        }
157
158
        /** @var IResolver $resolver */
159
        $resolver = $this->getResolver();
160
        if ($output = $this->sendSignal('kernel.router', ['resolver' => $resolver]) instanceof IResponse) {
161
            return $output;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $output; (boolean) is incompatible with the return type documented by Micro\Micro::doRun of type Micro\Web\IResponse|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
162
        }
163
164
        /** @var IController|ICommand $app */
165
        $app = $resolver->getApplication();
166
        if ($output = $this->sendSignal('kernel.controller', ['application' => $app]) instanceof IResponse) {
167
            return $output;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $output; (boolean) is incompatible with the return type documented by Micro\Micro::doRun of type Micro\Web\IResponse|string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
168
        }
169
170
        $output = $app->action((string)$resolver->getAction());
0 ignored issues
show
Bug introduced by
The method action does only exist in Micro\Mvc\Controllers\IController, but not in Micro\Base\ICommand.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
171
        if (!$output instanceof IOutput) {
172
            $response = $this->container->response ?: new Response;
0 ignored issues
show
Bug introduced by
Accessing response on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
173
            $response->setBody((string)$output);
174
            $output = $response;
175
        }
176
177
        $this->sendSignal('kernel.response', ['output' => $output]);
178
179
        return $output;
180
    }
181
182
    /**
183
     * Initialization container
184
     *
185
     * @access protected
186
     * @return void
187
     */
188
    protected function initializeContainer()
189
    {
190
        $class = $this->getContainerClass();
191
        if ($class) {
192
            $class = new $class;
193
        }
194
195
        $this->container = ($class instanceof IContainer) ? $class : new Container;
196
        $this->container->kernel = $this;
0 ignored issues
show
Bug introduced by
Accessing kernel on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
197
        $this->container->load($this->getConfig());
198
199
        if (false === $this->container->dispatcher) {
0 ignored issues
show
Bug introduced by
Accessing dispatcher on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
200
            $this->container->dispatcher = new Dispatcher;
0 ignored issues
show
Bug introduced by
Accessing dispatcher on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
201
        }
202
203
        $this->container->dispatcher->signal('kernel.boot', ['container' => $this->container]);
0 ignored issues
show
Bug introduced by
Accessing dispatcher on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
204
205
        $this->loaded = true;
206
    }
207
208
    /**
209
     * Get full class name
210
     * @return string
211
     */
212
    protected function getContainerClass()
213
    {
214
        return '';
215
    }
216
217
    /**
218
     * Default config path
219
     *
220
     * @return string
221
     */
222
    protected function getConfig()
223
    {
224
        return $this->getAppDir() . '/configs/index.php';
225
    }
226
227
    /**
228
     * Get application directory
229
     *
230
     * @return string
231
     */
232
    public function getAppDir()
233
    {
234
        if (!$this->appDir) {
235
            $this->appDir = realpath(dirname((new \ReflectionObject($this))->getFileName()));
236
        }
237
238
        return $this->appDir;
239
    }
240
241
    /**
242
     * Get web root directory
243
     *
244
     * @return string
245
     */
246
    public function getWebDir()
247
    {
248
        return $this->webDir;
249
    }
250
251
    /**
252
     * Add listener on event
253
     *
254
     * @access public
255
     *
256
     * @param string $listener listener name
257
     * @param \Closure $event ['Object', 'method'] or callable
258
     * @param int|null $prior priority
259
     *
260
     * @return bool
261
     */
262
    protected function addListener($listener, $event, $prior = null)
263
    {
264
        if (!is_string($listener) || !$this->container) {
265
            return false;
266
        }
267
268
        $this->container->dispatcher->addListener($listener, $event, $prior);
0 ignored issues
show
Bug introduced by
Accessing dispatcher on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
269
270
        return true;
271
    }
272
273
    /**
274
     * Send signal to dispatcher
275
     *
276
     * @param string $signal
277
     * @param $params
278
     * @return mixed
279
     */
280
    protected function sendSignal($signal, $params)
281
    {
282
        return $this->container->dispatcher->signal($signal, $params);
0 ignored issues
show
Bug introduced by
Accessing dispatcher on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
283
    }
284
285
    /**
286
     * Get resolver
287
     *
288
     * @access protected
289
     *
290
     * @return IResolver
291
     * @throws \Micro\Base\Exception
292
     */
293
    protected function getResolver()
294
    {
295
        if ($this->container->request->isCli()) {
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
296
            $resolver = $this->container->consoleResolver ?: '\Micro\resolver\ConsoleResolver';
0 ignored issues
show
Bug introduced by
Accessing consoleResolver on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
297
        } else {
298
            $resolver = $this->container->resolver ?: '\Micro\Resolver\HMVCResolver';
0 ignored issues
show
Bug introduced by
Accessing resolver on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
299
        }
300
301
        if (is_string($resolver) && is_subclass_of($resolver, '\Micro\Resolver\IResolver')) {
302
            $resolver = new $resolver($this->container);
303
        }
304
305
        if (!$resolver instanceof IResolver) {
306
            throw new Exception('Resolver is not implement an IResolver');
307
        }
308
309
        return $resolver;
310
    }
311
312
    /**
313
     * Do exception
314
     *
315
     * @access private
316
     *
317
     * @param \Exception $e Exception
318
     *
319
     * @return IOutput
320
     * @throws \Micro\base\Exception
321
     */
322
    private function doException(\Exception $e)
323
    {
324
        $output = $this->container->request->isCli() ? new DefaultConsoleCommand([]) : new Response();
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
325
326
        if ($this->container->request->isCli()) {
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
327
            $output->data = '"Error #' . $e->getCode() . ' - ' . $e->getMessage() . '"';
328
            $output->execute();
0 ignored issues
show
Bug introduced by
The method execute does only exist in Micro\Cli\Consoles\DefaultConsoleCommand, but not in Micro\Web\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
329
330
            return $output;
331
        }
332
        if (!$this->container->errorController || !$this->container->errorAction) {
0 ignored issues
show
Bug introduced by
Accessing errorController on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing errorAction on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
333
            $output->setBody('Option `errorController` or `errorAction` not configured');
0 ignored issues
show
Bug introduced by
The method setBody does only exist in Micro\Web\Response, but not in Micro\Cli\Consoles\DefaultConsoleCommand.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
334
335
            return $output;
336
        }
337
        $this->container->request->setPost('error', $e);
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
338
339
        $controller = $this->container->errorController;
0 ignored issues
show
Bug introduced by
Accessing errorController on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
340
341
        /** @var \Micro\mvc\controllers\IController $result */
342
        $result = new $controller($this->container, false);
343
        $result = $result->action($this->container->errorAction);
0 ignored issues
show
Bug introduced by
Accessing errorAction on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
344
        if ($result instanceof IOutput) {
345
            return $result;
346
        }
347
348
        $output->setBody((string)$result);
349
350
        return $output;
351
    }
352
353
    /**
354
     * Get start time
355
     *
356
     * @access public
357
     *
358
     * @return double
359
     */
360
    public function getStartTime()
361
    {
362
        return $this->startTime;
363
    }
364
365
    /**
366
     * Terminate application
367
     *
368
     * @access public
369
     *
370
     * @return void
371
     */
372
    public function terminate()
373
    {
374
        $this->container->dispatcher->signal('kernel.kill', ['container' => $this->container]);
0 ignored issues
show
Bug introduced by
Accessing dispatcher on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
375
    }
376
377
    /**
378
     * Get status of debug
379
     *
380
     * @access public
381
     *
382
     * @return bool
383
     */
384
    public function isDebug()
385
    {
386
        return $this->debug;
387
    }
388
389
    /**
390
     * Get components container
391
     *
392
     * @access public
393
     *
394
     * @return IContainer
395
     */
396
    public function getContainer()
397
    {
398
        return $this->container;
399
    }
400
401
    /**
402
     * Get character set
403
     *
404
     * @access public
405
     *
406
     * @return string
407
     */
408
    public function getCharset()
409
    {
410
        return 'UTF-8';
411
    }
412
413
    /**
414
     * Get logs directory
415
     *
416
     * @return string
417
     */
418
    public function getLogDir()
419
    {
420
        return $this->getAppDir() . '/logs';
421
    }
422
423
    /**
424
     * Get cache directory
425
     *
426
     * @return string
427
     */
428
    public function getCacheDir()
429
    {
430
        return $this->getAppDir() . '/cache/' . $this->getEnvironment();
431
    }
432
433
    /**
434
     * Get environment name
435
     *
436
     * @access public
437
     *
438
     * @return string
439
     */
440
    public function getEnvironment()
441
    {
442
        return $this->environment;
443
    }
444
}
445