Passed
Pull Request — 0.9.x (#272)
by Nikita
11:35
created

PhpSpyCompatibleParser::parsePhpSpyCompatible()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 9.8666
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
18
final class PhpSpyCompatibleParser
19
{
20
    /**
21
     * @param resource $fp
22
     * @return iterable<ParsedCallTrace>
23
     */
24
    public function parseFile($fp): iterable
25
    {
26
        $buffer = [];
27
        while (($line = fgets($fp)) !== false) {
28
            $line = trim($line);
29
            if ($line !== '') {
30
                $buffer[] = $line;
31
                continue;
32
            }
33
            yield $this->parsePhpSpyCompatible($buffer);
34
            $buffer = [];
35
        }
36
    }
37
38
    /** @param string[] $buffer */
39
    private function parsePhpSpyCompatible(array $buffer): ParsedCallTrace
40
    {
41
        $frames = [];
42
        foreach ($buffer as $line_buffer) {
43
            $result = explode(' ', $line_buffer);
44
            [$depth, $name, $file_line] = $result;
45
            if ($depth === '#') { // comment
46
                continue;
47
            }
48
            [$file, $line] = explode(':', $file_line);
49
            $frames[] = new ParsedCallFrame(
50
                $name,
51
                $file,
52
                Cast::toInt($line),
53
            );
54
        }
55
        return new ParsedCallTrace(...$frames);
56
    }
57
}
58