Failed Conditions
Push — master ( 5aed68...741277 )
by Mickael
13s queued 11s
created

FileParser::parse()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
c 0
b 0
f 0
rs 8.6737
nc 5
cc 6
nop 1
1
<?php
2
3
/**
4
 * Author: mickael Louzet @micklouzet
5
 * File: FileParser.php
6
 * Created: 05/12/2019
7
 */
8
9
declare(strict_types=1);
10
11
namespace ComposerLockFileParser;
12
13
use ComposerLockFileParser\ParserCollection;
14
use Symfony\Component\PropertyAccess\PropertyAccess;
15
16
class FileParser implements FileParserInterface
0 ignored issues
show
Coding Style introduced by
FileParser does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21
    public static function parse(string $filePath): ParserCollection
22
    {
23
        if (!\is_file($filePath)) {
24
            return new ParserCollection();
25
        }
26
27
        if (!$json = \file_get_contents($filePath)) {
28
            return new ParserCollection();
29
        }
30
31
        $data = \json_decode($json, true);
32
        $components = [];
33
34
        if (!$data || !isset($data['packages'])) {
35
            return new ParserCollection();
36
        }
37
        foreach ($data['packages'] as $vendor => $package) {
38
            $accessor = PropertyAccess::createPropertyAccessorBuilder()
39
                ->enableExceptionOnInvalidIndex()
40
                ->getPropertyAccessor();
41
42
            $array = [];
43
44
            $component = [
45
                'name' => $package['name'],
46
                'version' => $package['version'],
47
                'source' => $package['source'],
48
                'require' => $package['require'] ?? [],
49
                'description' => $package['description'],
50
                'keywords' => $package['keywords'],
51
                'time' => $package['time'],
52
            ];
53
54
            $accessor->setValue($array, "[name]", $component);
55
            $components[] = $component;
56
        }
57
58
        return new ParserCollection($components);
59
    }
60
61
    // public function byVendor(string $vendor)
62
    // {
63
    //     if (0 !== \strpos($package['name'], "vendor/")) {
64
    //         continue;
65
    //     }
66
    // }
67
}
68