Passed
Push — developer ( be0222...346aa7 )
by Mariusz
41:58 queued 06:52
created

AppException::view()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 4
nop 1
1
<?php
2
/**
3
 * Main exceptions file.
4
 *
5
 * @package App
6
 *
7
 * @see      http://php.net/manual/en/class.exception.php
8
 *
9
 * @copyright YetiForce Sp. z o.o.
10
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
11
 * @author    Mariusz Krzaczkowski <[email protected]>
12
 */
13
14
namespace App\Exceptions;
15
16
/**
17
 * Main exceptions class.
18
 */
19
class AppException extends \Exception
20
{
21
	/** @var string Default error exception template. */
22
	public $tplName = 'Exception.tpl';
23
24
	/**
25
	 * Construct the exception.
26
	 *
27
	 * @param string     $message
28
	 * @param int        $code
29
	 * @param \Throwable $previous
0 ignored issues
show
Documentation introduced by
Should the type for parameter $previous not be null|\Throwable?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
30
	 */
31
	public function __construct($message, $code = 200, \Throwable $previous = null)
32
	{
33
		parent::__construct($message, $code, $previous);
34
		$this->backtrace = \App\Debug::getBacktrace();
0 ignored issues
show
Bug introduced by
The property backtrace 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...
35
	}
36
37
	/**
38
	 * Show view.
39
	 *
40
	 * @param \Throwable $e
41
	 *
42
	 * @return void
43
	 */
44
	public static function view(\Throwable $e): void
45
	{
46
		$viewer = new \App\Viewer();
47
		if (\App\Config::get('displayDetailsException')) {
48
			$viewer->assign('MESSAGE', $e->getMessage());
49
			$viewer->assign('BACKTRACE', empty($e->backtrace) ? $e->getTraceAsString() : $e->backtrace);
0 ignored issues
show
Bug introduced by
Accessing backtrace 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...
50
			$viewer->assign('SESSION', $_SESSION);
51
		}
52
		$viewer->assign('CODE', $e->getCode());
53
		$viewer->assign('CSS_FILE', [
54
			PUBLIC_DIRECTORY . 'libraries/@fortawesome/fontawesome-free/css/all.css',
55
			PUBLIC_DIRECTORY . 'libraries/@mdi/font/css/materialdesignicons.css',
56
			PUBLIC_DIRECTORY . 'libraries/bootstrap/dist/css/bootstrap.css',
57
			PUBLIC_DIRECTORY . 'layouts/' . \App\Viewer::getLayoutName() . '/skins/basic/Main.css',
58
		]);
59
		$viewer->view($e->tplName);
0 ignored issues
show
Bug introduced by
Accessing tplName 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...
60
		if (\App\Config::$debugApi && \App\Session::has('debugApi') && \App\Session::get('debugApi')) {
61
			$viewer->assign('DEBUG_API', \App\Session::get('debugApi'));
62
			$viewer->assign('COLLAPSE', true);
63
			$viewer->view('DebugApi.tpl');
64
			\App\Session::set('debugApi', false);
65
		}
66
	}
67
}
68