Completed
Push — master ( 296743...12eab9 )
by Kirill
36:17
created

BaseSchemaException::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Exceptions;
11
12
use Illuminate\Support\Str;
13
use Railt\SDL\Runtime\CallStackInterface;
14
use Railt\SDL\Runtime\CallStackRenderer;
15
16
/**
17
 * Class BaseSchemaException
18
 */
19
abstract class BaseSchemaException extends \DomainException implements SchemaException
20
{
21
    /**
22
     * @var int
23
     */
24
    private $column;
25
26
    /**
27
     * @var CallStackRenderer
28
     */
29
    private $renderer;
30
31
    /**
32
     * BaseSchemaException constructor.
33
     * @param string $message
34
     * @param CallStackInterface $stack
35
     * @param \Throwable|null $previous
36
     */
37 3291
    public function __construct(string $message, CallStackInterface $stack, \Throwable $previous = null)
38
    {
39 3291
        parent::__construct(Str::ucfirst($message), 0, $previous);
40
41 3291
        $this->renderer = new CallStackRenderer($stack, \debug_backtrace());
42
43 3291
        $latest = $this->renderer->getLastRenderer();
44
45 3291
        $this->file   = $latest->getFile();
46 3291
        $this->line   = $latest->getLine();
47 3291
        $this->column = $latest->getColumn();
48 3291
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getColumn(): int
54
    {
55
        return $this->column;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 2280
    private function getHeader(): string
62
    {
63 2280
        return \vsprintf('%s: %s %s', [
64 2280
            static::class,
65 2280
            $this->getMessage(),
66 2280
            $this->renderer->getLastRenderer()->toMessageString(),
67
        ]);
68
    }
69
70
    /**
71
     * @return string
72
     */
73 2280
    public function __toString(): string
74
    {
75 2280
        $result[] = $this->getHeader();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
76
77 2280
        foreach ($this->renderer->getTrace() as $i => $item) {
78 2280
            $result[] = $item->toTraceString($i);
79
        }
80
81 2280
        return \implode(\PHP_EOL, $result);
82
    }
83
}
84