Skip to content

Commit a514d86

Browse files
committed
Add automated coding standard checks and Static Analysis
1 parent 15c7cb3 commit a514d86

File tree

7 files changed

+110
-69
lines changed

7 files changed

+110
-69
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/vendor
22
composer.phar
33
composer.lock
4+
.phpunit.result.cache
45
.DS_Store
56
.idea/*

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ before_script:
99
- curl -s http://getcomposer.org/installer | php
1010
- php composer.phar install --dev
1111

12-
script: php ./vendor/bin/phpunit
12+
script:
13+
- ./vendor/bin/phpunit
14+
- ./vendor/bin/phpcs --standard=phpcs.xml src
15+
- ./vendor/bin/phpstan --level=0 --no-progress analyse src

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"require": {
2121
"php": "^7.2",
2222
"illuminate/support": "^6.0|^7.0",
23-
"illuminate/console": "^6.0|^7.0"
23+
"illuminate/console": "^6.0|^7.0",
24+
"illuminate/cache": "^6.0|^7.0"
2425
},
2526
"suggest": {
2627
"geoip2/geoip2": "Required to use the MaxMind database or web service with GeoIP (~2.1).",
@@ -30,7 +31,9 @@
3031
"phpunit/phpunit": "^8.0",
3132
"mockery/mockery": "^1.3",
3233
"geoip2/geoip2": "~2.1",
33-
"vlucas/phpdotenv": "^4.0"
34+
"vlucas/phpdotenv": "^4.0",
35+
"phpstan/phpstan": "^0.12.14",
36+
"squizlabs/php_codesniffer": "^3.5"
3437
},
3538
"autoload": {
3639
"files": [

phpcs.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PSR2-package">
3+
<description>The PSR2 standard</description>
4+
5+
<!-- use preg_match https://github.com/squizlabs/PHP_CodeSniffer/issues/742#issuecomment-215250517 -->
6+
<exclude-pattern>/resources/</exclude-pattern>
7+
<exclude-pattern>/vendor/</exclude-pattern>
8+
<exclude-pattern>/_[a-zA-Z0-9\._]+\.php</exclude-pattern>
9+
<exclude-pattern>/\.[a-zA-Z0-9\._]+\.php</exclude-pattern>
10+
<exclude-pattern>\.git</exclude-pattern>
11+
12+
<!-- Include the whole PSR2 standard -->
13+
<rule ref="PSR2">
14+
<exclude name="PSR1.Methods.CamelCapsMethodName"/>
15+
<exclude name="PSR2.Files.EndFileNewline"/>
16+
<exclude name="PSR2.Methods.FunctionCallSignature.MultipleArguments"/>
17+
<exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseBrace"/>
18+
</rule>
19+
20+
<!-- Lines can be longer -->
21+
<rule ref="Generic.Files.LineLength">
22+
<properties>
23+
<property name="lineLimit" value="9999"/>
24+
</properties>
25+
</rule>
26+
</ruleset>

phpstan.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
parameters:
2+
excludes_analyse:
3+
- vendor
4+
- resources
5+
ignoreErrors:
6+
- '#Function config_path not found#'
7+
- '#Function config not found#'
8+
- '#Instantiated class Monolog\\Logger not found#'
9+
- '#Instantiated class Monolog\\Handler\\StreamHandler not found#'
10+
- '#Access to constant ERROR on an unknown class Monolog\\Logger#'

src/GeoIP.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ public function __construct(array $config, CacheManager $cache)
107107
* @param string $ip
108108
*
109109
* @return \Torann\GeoIP\Location
110+
* @throws \Exception
110111
*/
111112
public function getLocation($ip = null)
112113
{
113114
// Get location data
114115
$this->location = $this->find($ip);
115116

116117
// Should cache location
117-
if ($this->shouldCache($ip, $this->location)) {
118+
if ($this->shouldCache($this->location, $ip)) {
118119
$this->getCache()->set($ip, $this->location);
119120
}
120121

@@ -148,16 +149,15 @@ private function find($ip = null)
148149
$location = $this->getService()->locate($ip);
149150

150151
// Set currency if not already set by the service
151-
if (!$location->currency) {
152+
if (! $location->currency) {
152153
$location->currency = $this->getCurrency($location->iso_code);
153154
}
154155

155156
// Set default
156157
$location->default = false;
157158

158159
return $location;
159-
}
160-
catch (\Exception $e) {
160+
} catch (\Exception $e) {
161161
if ($this->config('log_failures', true) === true) {
162162
$log = new Logger('geoip');
163163
$log->pushHandler(new StreamHandler(storage_path('logs/geoip.log'), Logger::ERROR));
@@ -262,8 +262,8 @@ public function getClientIP()
262262
*/
263263
private function isValid($ip)
264264
{
265-
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
266-
&& !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE)
265+
if (! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
266+
&& ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE)
267267
) {
268268
return false;
269269
}
@@ -274,20 +274,19 @@ private function isValid($ip)
274274
/**
275275
* Determine if the location should be cached.
276276
*
277-
* @param string $ip
278-
* @param Location $location
277+
* @param Location $location
278+
* @param string|null $ip
279279
*
280280
* @return bool
281281
*/
282-
private function shouldCache($ip = null, Location $location)
282+
private function shouldCache(Location $location, $ip = null)
283283
{
284284
if ($location->default === true || $location->cached === true) {
285285
return false;
286286
}
287287

288288
switch ($this->config('cache', 'none')) {
289289
case 'all':
290-
return true;
291290
case 'some' && $ip === null:
292291
return true;
293292
}

src/Services/IPGeoLocation.php

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,55 @@
1-
<?php
2-
3-
namespace Torann\GeoIP\Services;
4-
5-
use Exception;
6-
use Illuminate\Support\Arr;
7-
use Torann\GeoIP\Support\HttpClient;
8-
9-
class IPGeoLocation extends AbstractService
10-
{
11-
/**
12-
* Http client instance.
13-
*
14-
* @var HttpClient
15-
*/
16-
protected $client;
17-
18-
/**
19-
* The "booting" method of the service.
20-
*
21-
* @return void
22-
*/
23-
public function boot()
24-
{
25-
$base = [
26-
'base_uri' => 'https://api.ipgeolocation.io/',
27-
];
28-
29-
if ($this->config('key')) {
30-
$base['base_uri'] = "{$base['base_uri']}ipgeo?apiKey=" . $this->config('key');
31-
}
32-
33-
$this->client = new HttpClient($base);
34-
}
35-
36-
/**
37-
* {@inheritdoc}
38-
*/
39-
40-
public function locate($ip)
41-
{
42-
// Get data from client
43-
$data = $this->client->get('&ip=' . $ip);
44-
45-
// Verify server response
46-
if ($this->client->getErrors() !== null) {
47-
throw new Exception('Request failed (' . $this->client->getErrors() . ')');
48-
}
49-
50-
// Parse body content
51-
$json = json_decode($data[0], true);
52-
53-
return $this->hydrate($json);
54-
55-
}
56-
}
1+
<?php
2+
3+
namespace Torann\GeoIP\Services;
4+
5+
use Exception;
6+
use Illuminate\Support\Arr;
7+
use Torann\GeoIP\Support\HttpClient;
8+
9+
class IPGeoLocation extends AbstractService
10+
{
11+
/**
12+
* Http client instance.
13+
*
14+
* @var HttpClient
15+
*/
16+
protected $client;
17+
18+
/**
19+
* The "booting" method of the service.
20+
*
21+
* @return void
22+
*/
23+
public function boot()
24+
{
25+
$base = [
26+
'base_uri' => 'https://api.ipgeolocation.io/',
27+
];
28+
29+
if ($this->config('key')) {
30+
$base['base_uri'] = "{$base['base_uri']}ipgeo?apiKey=" . $this->config('key');
31+
}
32+
33+
$this->client = new HttpClient($base);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
40+
public function locate($ip)
41+
{
42+
// Get data from client
43+
$data = $this->client->get('&ip=' . $ip);
44+
45+
// Verify server response
46+
if ($this->client->getErrors() !== null) {
47+
throw new Exception('Request failed (' . $this->client->getErrors() . ')');
48+
}
49+
50+
// Parse body content
51+
$json = json_decode($data[0], true);
52+
53+
return $this->hydrate($json);
54+
}
55+
}

0 commit comments

Comments
 (0)