Completed
Pull Request — master (#8)
by Mickael
02:27
created

FileParser::parse()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 5
nop 1
dl 0
loc 38
rs 8.9137
c 0
b 0
f 0
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 ComposerLockParser;
12
13
use ComposerLockFileParser\ParserCollection;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ComposerLockParser\ParserCollection. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Symfony\Component\PropertyAccess\PropertyAccess;
15
16
class FileParser implements FileParserInterface
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