Completed
Push — master ( 0a4015...907089 )
by Tobias
01:30
created

src/Model/SourceLocation.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 31
    public function __construct(string $message, string $path, int $line, array $context = [])
28
    {
29 31
        $this->message = $message;
30 31
        $this->path = (string) $path;
31 31
        $this->line = $line;
32 31
        $this->context = $context;
33 31
    }
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
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 30
    public function getMessage(): string
51
    {
52 30
        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