diff --git a/src/NpmStats.php b/src/NpmStats.php index 381520e..c9cc361 100644 --- a/src/NpmStats.php +++ b/src/NpmStats.php @@ -11,11 +11,11 @@ class NpmStats /** @var string */ protected $baseUrl; - const LAST_DAY = "last-day"; - const LAST_WEEK = "last-week"; - const LAST_MONTH = "last-month"; - const LAST_YEAR = "last-year"; - const TOTAL = "total"; + const LAST_DAY = 'last-day'; + const LAST_WEEK = 'last-week'; + const LAST_MONTH = 'last-month'; + const LAST_YEAR = 'last-year'; + const TOTAL = 'total'; /** * @param \GuzzleHttp\Client $client @@ -32,7 +32,7 @@ public function __construct(Client $client, $baseUrl = 'https://api.npmjs.org/do * @param string $pointValue * @return array */ - private function getStatsByPoint($packageName, $pointValue = "last-day") + private function getStatsByPoint($packageName, $pointValue = 'last-day') { return $this->makeRequest("/point/{$pointValue}/{$packageName}"); } @@ -42,7 +42,7 @@ private function getStatsByPoint($packageName, $pointValue = "last-day") * @param string $rangeValue * @return array */ - private function getStatsByRange($packageName, $rangeValue = "last-day") + private function getStatsByRange($packageName, $rangeValue = 'last-day') { return $this->makeRequest("/range/{$rangeValue}/{$packageName}"); } @@ -54,7 +54,7 @@ private function getStatsByRange($packageName, $rangeValue = "last-day") * @return array * @throws \Exception */ - public function getStats($packageName, $period = "last-day", $asRange = false) + public function getStats($packageName, $period = 'last-day', $asRange = false) { if (empty($packageName)) { throw new \Exception("Package name can't be empty"); @@ -69,7 +69,6 @@ public function getStats($packageName, $period = "last-day", $asRange = false) return $this->getStatsByPoint($packageName, $period); } - /** * @param string $resource * @param array $query @@ -89,14 +88,12 @@ private function makeRequest($resource, array $query = []) private function replaceSpecialPeriodsIfApplicable(&$period) { if ($period === self::TOTAL) { - $currentDate = (new \DateTime)->format("Y-m-d"); + $currentDate = (new \DateTime)->format('Y-m-d'); $period = "2015-01-01:{$currentDate}"; - } else if ($period === self::LAST_YEAR) { - $currentDate = (new \DateTime)->format("Y-m-d"); - $beforeDate = (new \DateTime)->modify("-365 days")->format("Y-m-d"); + } elseif ($period === self::LAST_YEAR) { + $currentDate = (new \DateTime)->format('Y-m-d'); + $beforeDate = (new \DateTime)->modify('-365 days')->format('Y-m-d'); $period = "{$beforeDate}:{$currentDate}"; } - - return; } -} \ No newline at end of file +} diff --git a/tests/NpmStatsTest.php b/tests/NpmStatsTest.php index b5ede77..17973f6 100644 --- a/tests/NpmStatsTest.php +++ b/tests/NpmStatsTest.php @@ -12,7 +12,7 @@ class NpmStatsTest extends TestCase /** @var \Developmint\NpmStats\NpmStats */ protected $npmStats; - public function setUp() : void + public function setUp(): void { $client = new Client(); @@ -30,7 +30,7 @@ public function testItCanRetrievePointStats() $this->assertArrayHasKey('downloads', $result); $this->assertArrayHasKey('start', $result); $this->assertArrayHasKey('end', $result); - $this->assertEquals($packageName, $result["package"]); + $this->assertEquals($packageName, $result['package']); } public function testItCanRetrievePointBulkStats() @@ -51,10 +51,10 @@ public function testItCanRetrieveRangeStats() $this->assertArrayHasKey('start', $result); $this->assertArrayHasKey('end', $result); - $this->assertEquals($packageName, $result["package"]); + $this->assertEquals($packageName, $result['package']); $this->assertArrayHasKey('downloads', $result); - $this->assertArrayHasKey('downloads', $result["downloads"][0]); - $this->assertArrayHasKey('day', $result["downloads"][0]); + $this->assertArrayHasKey('downloads', $result['downloads'][0]); + $this->assertArrayHasKey('day', $result['downloads'][0]); } public function testItCanRetrieveYearlyStats() @@ -63,11 +63,10 @@ public function testItCanRetrieveYearlyStats() $result = $this->npmStats->getStats($packageName, NpmStats::LAST_YEAR); $this->assertArrayHasKey('start', $result); $this->assertArrayHasKey('end', $result); - $this->assertEquals((new \DateTime)->modify("-365 days")->format("Y-m-d"), $result["start"]); + $this->assertEquals((new \DateTime)->modify('-365 days')->format('Y-m-d'), $result['start']); - $this->assertEquals($packageName, $result["package"]); + $this->assertEquals($packageName, $result['package']); $this->assertArrayHasKey('downloads', $result); - } public function testItCanRetrieveYearlyStatsForBulk() @@ -84,7 +83,7 @@ public function testItCanRetrieveAllTimeStats() $result = $this->npmStats->getStats($packageName, NpmStats::TOTAL); $this->assertArrayHasKey('start', $result); $this->assertArrayHasKey('end', $result); - $this->assertEquals($packageName, $result["package"]); + $this->assertEquals($packageName, $result['package']); $this->assertArrayHasKey('downloads', $result); }