StatFetcher   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 134
Duplicated Lines 22.39 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
dl 30
loc 134
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDownloadsLastDay() 0 9 1
A __construct() 0 3 2
A getDownloads() 0 11 1
A getDownloadsLastWeek() 9 9 1
A getDownloadsLastYear() 9 9 1
A getDownloadsLastMonth() 9 9 1
A getDownloadsBetweenDates() 0 8 1
A getDownloadsInDateRange() 0 12 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
2
3
namespace Vanbrabantf\NpmStatFetcher;
4
5
use Cake\Chronos\Chronos;
6
use DateTimeInterface;
7
use Vanbrabantf\NpmStatFetcher\Dates\DateChecker;
0 ignored issues
show
Bug introduced by
The type Vanbrabantf\NpmStatFetcher\Dates\DateChecker was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Vanbrabantf\NpmStatFetcher\Dates\DateRange;
9
use Vanbrabantf\NpmStatFetcher\Package\Package;
10
use Vanbrabantf\NpmStatFetcher\Statistics\DownloadStatistics;
11
12
class StatFetcher
13
{
14
    /**
15
     * @var NpmRegistryRepository
16
     */
17
    private $repository;
18
19
    /**
20
     * @param NpmRegistryRepository $repository
21
     */
22
    public function __construct(NpmRegistryRepository $repository = null)
23
    {
24
        $this->repository = $repository ?: new NpmRegistryRepository(ClientFactory::Build());
25
    }
26
27
28
    /**
29
     * @param string $packageName
30
     *
31
     * @return DownloadStatistics
32
     */
33
    public function getDownloadsLastDay(string $packageName): DownloadStatistics
34
    {
35
        $package = new Package($packageName);
36
37
        $resource = $this->repository->getResourceByPath(
38
            '/downloads/point/last-day/' . $package
39
        );
40
41
        return DownloadStatistics::fromJson($package, $resource);
42
    }
43
44
    /**
45
     * @param string $packageName
46
     *
47
     * @return DownloadStatistics
48
     */
49 View Code Duplication
    public function getDownloadsLastWeek(string $packageName): DownloadStatistics
0 ignored issues
show
Duplication introduced by
This method 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...
50
    {
51
        $package = new Package($packageName);
52
53
        $resource = $this->repository->getResourceByPath(
54
            '/downloads/point/last-week/' . $package
55
        );
56
57
        return DownloadStatistics::fromJson($package, $resource);
58
    }
59
60
    /**
61
     * @param string $packageName
62
     *
63
     * @return DownloadStatistics
64
     */
65 View Code Duplication
    public function getDownloadsLastMonth(string $packageName): DownloadStatistics
0 ignored issues
show
Duplication introduced by
This method 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...
66
    {
67
        $package = new Package($packageName);
68
69
        $resource = $this->repository->getResourceByPath(
70
            '/downloads/point/last-month/' . $package
71
        );
72
73
        return DownloadStatistics::fromJson($package, $resource);
74
    }
75
76
    /**
77
     * @param string $packageName
78
     *
79
     * @return DownloadStatistics
80
     */
81 View Code Duplication
    public function getDownloadsLastYear(string $packageName): DownloadStatistics
0 ignored issues
show
Duplication introduced by
This method 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...
82
    {
83
        $package = new Package($packageName);
84
85
        $resource = $this->repository->getResourceByPath(
86
            '/downloads/point/last-year/' . $package
87
        );
88
89
        return DownloadStatistics::fromJson($package, $resource);
90
    }
91
92
    /**
93
     * @param string $packageName
94
     *
95
     * @return DownloadStatistics
96
     */
97
    public function getDownloads(string $packageName): DownloadStatistics
98
    {
99
        $package = new Package($packageName);
100
        $dateRange = new DateRange(new Chronos('1999-01-01'), new Chronos());
101
102
        $resource = $this->repository->getResourceByPath(
103
            '/downloads/point/' . $dateRange->getStartDate()->format('Y-m-d') .
104
            ':' . $dateRange->getEndDate()->format('Y-m-d') . '/' . $package
105
        );
106
107
        return DownloadStatistics::fromJson($package, $resource);
108
    }
109
110
    /**
111
     * @param string $packageName
112
     * @param DateTimeInterface $start
113
     * @param DateTimeInterface $end
114
     *
115
     * @return DownloadStatistics
116
     */
117
    public function getDownloadsBetweenDates(
118
        string $packageName,
119
        DateTimeInterface $start,
120
        DateTimeInterface $end
121
    ): DownloadStatistics {
122
        return $this->getDownloadsInDateRange(
123
            $packageName,
124
            new DateRange($start, $end)
125
        );
126
    }
127
128
    /**
129
     * @param string $packageName
130
     * @param DateRange $dateRange
131
     *
132
     * @return DownloadStatistics
133
     */
134
    public function getDownloadsInDateRange(
135
        string $packageName,
136
        DateRange $dateRange
137
    ): DownloadStatistics {
138
        $package = new Package($packageName);
139
140
        $resource = $this->repository->getResourceByPath(
141
            '/downloads/point/' . $dateRange->getStartDate()->format('Y-m-d') .
142
            ':' . $dateRange->getEndDate()->format('Y-m-d') . '/' . $package
143
        );
144
145
        return DownloadStatistics::fromJson($package, $resource);
146
    }
147
}
148