Registry   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 50
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A analyzePackage() 20 20 2
A parseJson() 9 9 2

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
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\PackageManager;
13
14
use AppBundle\Entity\Package;
15
use GuzzleHttp\Client as GuzzleClient;
16
17 View Code Duplication
class Registry implements PackageManager
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
18
{
19
    /**
20
     * @var string
21
     */
22
    private $packageVendor = 'https://registry.npmjs.org/';
23
24
    /**
25
     * @param Package $package
26
     *
27
     * @return Package
28
     */
29
    public function analyzePackage(Package $package)
30
    {
31
        $client = new GuzzleClient();
32
        $response = $client->get(
33
            $this->packageVendor . $package->getName(),
34
            [
35
                'exceptions' => false,
36
            ]
37
        );
38
39
        if ($response->getStatusCode() == 200) {
40
            $data = $this->parseJson((string) $response->getBody());
41
42
            $newestVersion = $data['dist-tags']['latest'];
43
            $package->setLatestVersion($newestVersion);
44
            $package->setLastCheckAt(new \DateTime());
45
        }
46
47
        return $package;
48
    }
49
50
    /**
51
     * Parse JSON data.
52
     *
53
     * @param string $data
54
     *
55
     * @return mixed
56
     */
57
    private function parseJson($data)
58
    {
59
        $parsedData = json_decode($data, true);
60
        if ($parsedData === false) {
61
            throw new \RuntimeException('Unable to parse json file');
62
        }
63
64
        return $parsedData;
65
    }
66
}
67