Passed
Push — 0.9.x ( 571df9...c78c84 )
by Shinji
02:41 queued 56s
created

PhpSpyCompatibleParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 28
c 2
b 0
f 1
dl 0
loc 53
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A splitLineNumberAndFilePath() 0 8 1
A parseFile() 0 11 3
A parsePhpSpyCompatible() 0 18 3
1
<?php
2
3
/**
4
 * This file is part of the reliforp/reli-prof package.
5
 *
6
 * (c) sji <[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
declare(strict_types=1);
13
14
namespace Reli\Converter;
15
16
use PhpCast\Cast;
0 ignored issues
show
Bug introduced by
The type PhpCast\Cast 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...
17
use Webmozart\Assert\Assert;
0 ignored issues
show
Bug introduced by
The type Webmozart\Assert\Assert 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...
18
19
final class PhpSpyCompatibleParser
20
{
21
    /**
22
     * @param resource $fp
23
     * @return iterable<ParsedCallTrace>
24
     */
25
    public function parseFile($fp): iterable
26
    {
27
        $buffer = [];
28
        while (($line = fgets($fp)) !== false) {
29
            $line = trim($line);
30
            if ($line !== '') {
31
                $buffer[] = $line;
32
                continue;
33
            }
34
            yield $this->parsePhpSpyCompatible($buffer);
35
            $buffer = [];
36
        }
37
    }
38
39
    /** @param string[] $buffer */
40
    private function parsePhpSpyCompatible(array $buffer): ParsedCallTrace
41
    {
42
        $frames = [];
43
        foreach ($buffer as $line_buffer) {
44
            $result = explode(' ', $line_buffer);
45
            [$depth, $name, $file_line] = $result;
46
            if ($depth === '#') { // comment
47
                continue;
48
            }
49
            Assert::stringNotEmpty($file_line);
50
            [$file, $line] = $this->splitLineNumberAndFilePath($file_line);
51
            $frames[] = new ParsedCallFrame(
52
                $name,
53
                $file,
54
                $line,
55
            );
56
        }
57
        return new ParsedCallTrace(...$frames);
58
    }
59
60
    /**
61
     * @param non-empty-string $file_line
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
62
     * @return array{non-empty-string, int}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{non-empty-string, int} at position 2 could not be parsed: Expected ':' at position 2, but found 'non-empty-string'.
Loading history...
63
     */
64
    private function splitLineNumberAndFilePath(string $file_line): array
65
    {
66
        $separator_position = strrpos($file_line, ':');
67
        Assert::notFalse($separator_position);
68
        $line = substr($file_line, $separator_position + 1);
69
        $file = substr($file_line, 0, $separator_position);
70
        Assert::stringNotEmpty($file);
71
        return [$file, Cast::toInt($line)];
72
    }
73
}
74