SourceLocation::createHere()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Extractor\Model;
13
14
/**
15
 * @author Tobias Nyholm <[email protected]>
16
 */
17
final class SourceLocation
18
{
19
    /**
20
     * Translation key.
21
     */
22
    private $message;
23
    private $path;
24
    private $line;
25
    private $context;
26
27 35
    public function __construct(string $message, string $path, int $line, array $context = [])
28
    {
29 35
        $this->message = $message;
30 35
        $this->path = (string) $path;
31 35
        $this->line = $line;
32 35
        $this->context = $context;
33 35
    }
34
35
    /**
36
     * Create a source location from your current location.
37
     */
38 4
    public static function createHere(string $message, array $context = []): self
39
    {
40 4
        foreach (debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2) as $trace) {
41
            // File is not set if we call from an anonymous context like an array_map function.
42 4
            if (isset($trace['file'])) {
43 4
                break;
44
            }
45
        }
46
47 4
        return new self($message, $trace['file'], $trace['line'], $context);
0 ignored issues
show
Bug introduced by
The variable $trace seems to be defined by a foreach iteration on line 40. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
48
    }
49
50 34
    public function getMessage(): string
51
    {
52 34
        return $this->message;
53
    }
54
55 3
    public function getPath(): string
56
    {
57 3
        return $this->path;
58
    }
59
60 6
    public function getLine(): int
61
    {
62 6
        return $this->line;
63
    }
64
65 10
    public function getContext(): array
66
    {
67 10
        return $this->context;
68
    }
69
}
70