Npm::fetchDependencies()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 10
Ratio 43.48 %

Importance

Changes 0
Metric Value
dl 10
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 12
nc 5
nop 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of Packy.
5
 *
6
 * (c) Peter Nijssen
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
namespace AppBundle\DependencyManager;
13
14
use AppBundle\Entity\Project;
15
use AppBundle\RepositoryManager\RepositoryManager;
16
17
class Npm implements DependencyManager
18
{
19
    /**
20
     * @var string
21
     */
22
    private $packageFileName;
23
24
    /**
25
     * Constructor.
26
     */
27
    public function __construct()
28
    {
29
        $this->packageFileName = 'package-lock.json';
30
    }
31
32
    /**
33
     * Fetch the dependencies.
34
     *
35
     * @param RepositoryManager $repositoryManager
36
     * @param Project           $project
37
     *
38
     * @return array
39
     */
40
    public function fetchDependencies(RepositoryManager $repositoryManager, Project $project): array
41
    {
42
        $fileContent = $repositoryManager->getFileContents($project, $this->packageFileName);
43
44
        if (is_array($fileContent)) {
45
            $dependencies = [];
46 View Code Duplication
            if (array_key_exists('dependencies', $fileContent)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
                foreach ($fileContent['dependencies'] as $name => $package) {
48
                    $dependencies[$name] = $package['version'];
49
                }
50
            }
51
52 View Code Duplication
            if (array_key_exists('devDependencies', $fileContent)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
                foreach ($fileContent['devDependencies'] as $name => $package) {
54
                    $dependencies[$name] = $package['version'];
55
                }
56
            }
57
58
            return $dependencies;
59
        }
60
61
        return [];
62
    }
63
64
    /**
65
     * Get the name of the fetcher.
66
     *
67
     * @return string
68
     */
69
    public function getName(): string
70
    {
71
        return 'npm';
72
    }
73
}
74