BitrixException   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasException() 0 8 2
B generate() 0 12 5
1
<?php
2
/**
3
 * For the full copyright and license information, please view the LICENSE.md
4
 * file that was distributed with this source code.
5
 */
6
7
namespace Notamedia\ConsoleJedi\Application\Exception;
8
9
/**
10
 * Wrap for legacy bitrix core exceptions.
11
 *
12
 * Throws exception with $APPLICATION->GetException() message.
13
 */
14
class BitrixException extends \RuntimeException
15
{
16
    public static function hasException(\CMain $APPLICATION = null)
0 ignored issues
show
Coding Style introduced by
hasException uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
17
    {
18
        if (null === $APPLICATION) {
19
            $APPLICATION = $GLOBALS['APPLICATION'];
20
        }
21
22
        return is_object($APPLICATION->GetException());
23
    }
24
25
    /**
26
     * Check for legacy bitrix exception, throws new self if any
27
     *
28
     * @param string $message [optional] Additional error message
29
     * @param \CMain $APPLICATION [optional] $APPLICATION instance
30
     * @throws static
31
     */
32
    public static function generate($message = null, \CMain $APPLICATION = null)
0 ignored issues
show
Coding Style introduced by
generate uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
33
    {
34
        if (null === $APPLICATION) {
35
            $APPLICATION = $GLOBALS['APPLICATION'];
36
        }
37
38
        if ($ex = $APPLICATION->GetException()) {
39
            throw new static($message ? $message . ': ' . $ex->GetString() : $ex->GetString());
40
        } else {
41
            throw new static($message ? $message : 'Unknown exception');
42
        }
43
    }
44
}