Npm   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 17.54 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 10
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B fetchDependencies() 10 23 6
A getName() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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