Test Failed
Push — master ( 90252a...135589 )
by Waaaaaaaaaa
02:31
created

src/Frame.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
namespace Logfile;
4
5
class Frame
6
{
7
    use PathTrait;
0 ignored issues
show
The type Logfile\PathTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
    protected $file;
10
11
    protected $line;
12
13
    protected $caller;
14
15
    protected $args = [];
16
17 1
    public static function create(array $params): self
18
    {
19 1
        $frame = new self;
20
21 1
        if (isset($params['file'])) {
22 1
            $frame->setFile($params['file']);
23
        }
24
25 1
        if (isset($params['line'])) {
26 1
            $frame->setLine($params['line']);
27
        }
28
29 1
        if (isset($params['class'])) {
30 1
            $frame->setCaller(\sprintf('%s%s%s', $params['class'], $params['type'], $params['function']));
31 1
        } elseif (isset($params['function'])) {
32 1
            $frame->setCaller($params['function']);
33
        }
34
35 1
        if (isset($params['args'])) {
36 1
            $frame->setArguments($params['args']);
37
        } else {
38 1
            $frame->setArguments([]);
39
        }
40
41 1
        return $frame;
42
    }
43
44 3
    public function getRelativeFilepath(): string
45
    {
46 3
        if ($this->hasPath() && \strpos($this->getFile(), $this->getPath()) === 0) {
47 1
            return \mb_substr($this->getFile(), \mb_strlen($this->getPath()));
48
        }
49 2
        return $this->getFile();
50
    }
51
52 4
    public function getFile(): string
53
    {
54 4
        return $this->file;
55
    }
56
57 5
    public function setFile(string $file): void
58
    {
59 5
        $this->file = $file;
60 5
    }
61
62 3
    public function hasFile(): bool
63
    {
64 3
        return $this->file !== null && \is_readable($this->file);
65
    }
66
67 3
    public function getLine(): int
68
    {
69 3
        return $this->line;
70
    }
71
72 4
    public function setLine(int $line): void
73
    {
74 4
        $this->line = $line;
75 4
    }
76
77 3
    public function hasLine(): bool
78
    {
79 3
        return $this->line !== null;
80
    }
81
82 2
    public function hasCaller(): bool
83
    {
84 2
        return !empty($this->caller);
85
    }
86
87 2
    public function getCaller(): string
88
    {
89 2
        return $this->caller;
90
    }
91
92 3
    public function setCaller(string $caller): void
93
    {
94 3
        $this->caller = $caller;
95 3
    }
96
97 4
    public function getArguments(): array
98
    {
99 4
        return $this->args ?: [];
100
    }
101
102 4
    public function setArguments(array $args): void
103
    {
104 4
        $this->args = [];
105
106 4
        foreach (\array_values($args) as $index => $arg) {
107 4
            $this->args['param'.($index + 1)] = $this->normalise($arg);
108
        }
109 4
    }
110
111 2
    protected function normaliseArray($value): string
112
    {
113 2
        $count = count($value);
114
115 2
        if ($count > 100) {
116 1
            return 'Array of length ' . $count;
117
        }
118
119 1
        $types = [];
120
121 1
        foreach ($value as $item) {
122 1
            $type = gettype($item);
123 1
            if ('object' === $type) {
124 1
                $type = get_class($item);
125
            }
126 1
            if (!in_array($type, $types)) {
127 1
                $types[] = $type;
128
            }
129
        }
130
131 1
        if (count($types) > 3) {
132 1
            return 'Mixed Array of length ' . $count;
133
        }
134
135 1
        return 'Array<'.implode('|', $types).'> of length ' . $count;
136
    }
137
138 4
    protected function normalise($value): string
139
    {
140 4
        if ($value === null) {
141 1
            return 'null';
142 4
        } elseif ($value === false) {
143 1
            return 'false';
144 4
        } elseif ($value === true) {
145 2
            return 'true';
146 3
        } elseif (is_float($value) && (int) $value == $value) {
147 1
            return $value.'.0';
148 3
        } elseif (is_integer($value) || is_float($value)) {
149 1
            return (string) $value;
150 3
        } elseif (is_object($value) || gettype($value) == 'object') {
151 1
            return 'Object '.get_class($value);
152 3
        } elseif (is_resource($value)) {
153 1
            return 'Resource '.get_resource_type($value);
154 3
        } elseif (is_array($value)) {
155 2
            return $this->normaliseArray($value);
156
        }
157
158 1
        $truncation = new Truncation($value);
159 1
        return $truncation->truncate();
160
    }
161
162 2
    public function hasContext(): bool
163
    {
164 2
        return $this->hasFile() && $this->hasLine();
165
    }
166
167 2
    public function getContext(): Context
168
    {
169 2
        return new Context($this->getFile(), $this->getLine());
170
    }
171
172 1
    public function toArray(): array
173
    {
174 1
        $frame = [];
175
176 1
        if ($this->hasFile()) {
177 1
            $frame['file'] = $this->getRelativeFilepath();
178
        }
179
180 1
        if ($this->hasLine()) {
181 1
            $frame['line'] = $this->getLine();
182
        }
183
184 1
        if ($this->hasCaller()) {
185 1
            $frame['caller'] = $this->getCaller();
186
        }
187
188 1
        $frame['args'] = $this->getArguments();
189
190 1
        if ($this->hasContext()) {
191 1
            $frame['context'] = $this->getContext()->getPlaceInFile();
192
        }
193
194 1
        return $frame;
195
    }
196
}
197