Completed
Push — 1.x ( d7de5d...04d644 )
by Akihito
01:58
created

ExceptionAsString::detail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
ccs 9
cts 9
cp 1
cc 1
eloc 9
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package\Provide\Error;
8
9
use BEAR\Sunday\Extension\Router\RouterMatch as Request;
10
11
final class ExceptionAsString
12
{
13
    public function summery(\Exception $e, $log)
14
    {
15
        return sprintf("\n\n[%s]\n%s\n %s", get_class($e), $e->getMessage(), $log);
16
    }
17
18
    /**
19
     * @param \Exception $e
20
     * @param Request    $request
21
     *
22
     * @return string
23
     */
24 1
    public function detail(\Exception $e, Request $request)
0 ignored issues
show
Coding Style introduced by
detail uses the super-global variable $_SERVER 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...
25
    {
26 1
        $eSummery = sprintf(
27 1
            "%s(%s)\n in file %s on line %s\n\n%s",
28 1
            get_class($e),
29 1
            $e->getMessage(),
30 1
            $e->getFile(),
31 1
            $e->getLine(),
32 1
            $e->getTraceAsString()
33
        );
34
35 1
        return sprintf("%s\n%s\n\n%s\n%s\n\n", date(DATE_RFC2822), $request, $eSummery, $this->getPhpVariables($_SERVER));
36
    }
37
38
    /**
39
     * @param array $server
40
     *
41
     * @return string
42
     */
43 1
    private function getPhpVariables(array $server)
44
    {
45 1
        if (PHP_SAPI === 'cli') {
46
            //            return '';
47
        }
48
49
        return sprintf("\nPHP Variables\n\n\$_SERVER => %s", print_r($server, true)); // @codeCoverageIgnore
50
    }
51
}
52