Completed
Push — master ( f172d4...76e139 )
by devosc
02:14
created

src/Exception/Base.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Exception;
7
8
use Mvc5\Arg;
9
use Mvc5\Exception as _Exception;
10
11
trait Base
12
{
13
    /**
14
     * @param mixed|\Throwable $exception
15
     * @param int $offset
16
     * @return \Throwable
17
     */
18 31
    protected static function backtrace(\Throwable $exception, $offset = 2)
19
    {
20 31
        $offset && ($origin = $exception->getTrace()[$offset]) && isset($origin[Arg::FILE])
21 31
            && ($exception->file = $origin[Arg::FILE]) && ($exception->line = $origin[Arg::LINE]);
0 ignored issues
show
Accessing file on the interface Throwable 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...
Accessing line on the interface Throwable 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...
22
23 31
        return static::offset($exception, $offset);
24
    }
25
26
    /**
27
     * @param string $exception
28
     * @param array|string $params
29
     * @param int $offset
30
     * @return \Throwable
31
     */
32 31
    protected static function create($exception, $params = [], $offset = 2)
33
    {
34 31
        return static::backtrace(static::instance($exception, (array) $params), $offset);
35
    }
36
37
    /**
38
     * @param string $exception
39
     * @param array $params
40
     * @return \Throwable
41
     */
42 34
    protected static function instance($exception, array $params = [])
43
    {
44 34
        return new $exception(...$params);
45
    }
46
47
    /**
48
     * @param $exception
49
     * @param int $offset
50
     * @return \Throwable
51
     */
52 34
    protected static function offset($exception, $offset = 1)
53
    {
54 34
        $offset && $exception->offset = $offset;
55
56 34
        return $exception;
57
    }
58
59
    /**
60
     * @param $exception
61
     * @return mixed
62
     * @throws \Throwable
63
     */
64 38
    static function raise(\Throwable $exception)
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
    {
66 38
        throw $exception;
67
    }
68
}
69