Skip to content

Commit

Permalink
Add Rector and apply changes from it
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed Jan 10, 2025
1 parent e313d23 commit bfe60b6
Show file tree
Hide file tree
Showing 23 changed files with 206 additions and 231 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"orchestra/testbench": "^8.8 || ^9.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.5 || ^11.0",
"rector/rector": "^1.2",
"squizlabs/php_codesniffer": "^3.9",
"vimeo/psalm": "^5.25"
},
Expand Down Expand Up @@ -63,6 +64,7 @@
"phpcs": "vendor/bin/phpcs --standard=phpcs.xml src",
"phpstan": "vendor/bin/phpstan --level=0 --no-progress analyse --configuration phpstan.neon --memory-limit 2G",
"psalm": "vendor/bin/psalm",
"rector": "vendor/bin/rector process --config rector.php ",
"sa": "@psalm",
"sa:bl": "@psalm --set-baseline=psalm-baseline.xml",
"test": "vendor/bin/phpunit"
Expand Down
92 changes: 39 additions & 53 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="src/Cache.php">
<MixedInferredReturnType>
<code><![CDATA[bool]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$this->cache->flush()]]></code>
<code><![CDATA[$this->cache->flush()]]></code>
</MixedReturnStatement>
<PossiblyInvalidPropertyAssignmentValue>
<code><![CDATA[($tags === [] || !$cache->supportsTags()) ? $cache : $cache->tags($tags)]]></code>
</PossiblyInvalidPropertyAssignmentValue>
</file>
<file src="src/Console/Clear.php">
<DeprecatedMethod>
<code><![CDATA[fire]]></code>
</DeprecatedMethod>
<InvalidReturnStatement>
<code><![CDATA[$this->output->error('Default cache system does not support tags')]]></code>
</InvalidReturnStatement>
<MixedMethodCall>
<code><![CDATA[config]]></code>
<code><![CDATA[flush]]></code>
Expand All @@ -34,20 +26,24 @@
</PropertyNotSetInConstructor>
</file>
<file src="src/Console/Update.php">
<DeprecatedMethod>
<code><![CDATA[fire]]></code>
</DeprecatedMethod>
<MixedArgument>
<code><![CDATA[$result]]></code>
<code><![CDATA[$service]]></code>
<code><![CDATA[$service]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$result]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[int]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[getService]]></code>
</MixedMethodCall>
<MixedReturnStatement>
<code><![CDATA[static::FAILURE]]></code>
<code><![CDATA[static::FAILURE]]></code>
<code><![CDATA[static::SUCCESS]]></code>
<code><![CDATA[static::SUCCESS]]></code>
</MixedReturnStatement>
<NonInvariantDocblockPropertyType>
<code><![CDATA[$description]]></code>
</NonInvariantDocblockPropertyType>
Expand Down Expand Up @@ -76,22 +72,8 @@
<code><![CDATA[$this->remote_ip]]></code>
</DeprecatedProperty>
<DocblockTypeContradiction>
<code><![CDATA[$this->currencies === null]]></code>
<code><![CDATA[$this->service === null]]></code>
</DocblockTypeContradiction>
<InvalidPropertyAssignmentValue>
<code><![CDATA[new Cache(
$cache,
$this->config('cache_tags'),
$this->config('cache_expires', 30)
)]]></code>
</InvalidPropertyAssignmentValue>
<InvalidReturnStatement>
<code><![CDATA[$this->cache]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code><![CDATA[\InteractionDesignFoundation\GeoIP\Cache]]></code>
</InvalidReturnType>
<LessSpecificReturnStatement>
<code><![CDATA[$this->service]]></code>
</LessSpecificReturnStatement>
Expand All @@ -109,7 +91,7 @@
<code><![CDATA[$config]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[string]]></code>
<code><![CDATA[string|null]]></code>
</MixedInferredReturnType>
<MixedMethodCall>
<code><![CDATA[new $class($config)]]></code>
Expand All @@ -126,11 +108,8 @@
<PossiblyNullArgument>
<code><![CDATA[$ip]]></code>
<code><![CDATA[$location->iso_code]]></code>
<code><![CDATA[$this->currencies]]></code>
</PossiblyNullArgument>
<PossiblyNullPropertyAssignmentValue>
<code><![CDATA[null]]></code>
<code><![CDATA[null]]></code>
</PossiblyNullPropertyAssignmentValue>
<PropertyNotSetInConstructor>
<code><![CDATA[$service]]></code>
</PropertyNotSetInConstructor>
Expand All @@ -144,22 +123,17 @@
</RiskyTruthyFalsyComparison>
</file>
<file src="src/GeoIPServiceProvider.php">
<MissingClosureParamType>
<code><![CDATA[$app]]></code>
</MissingClosureParamType>
<MixedArgument>
<code><![CDATA[$app->config->get('geoip', [])]]></code>
<code><![CDATA[$app['cache']]]></code>
<code><![CDATA[$app['config']->get('geoip', [])]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$app['cache']]]></code>
</MixedArrayAccess>
<MixedMethodCall>
<code><![CDATA[get]]></code>
</MixedMethodCall>
<MixedPropertyFetch>
<code><![CDATA[$app->config]]></code>
</MixedPropertyFetch>
<UndefinedInterfaceMethod>
<code><![CDATA[$app]]></code>
<code><![CDATA[$app]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="src/Location.php">
<InvalidParamDefault>
Expand All @@ -169,9 +143,6 @@
<code><![CDATA[$key]]></code>
<code><![CDATA[$value]]></code>
</MissingParamType>
<MissingTemplateParam>
<code><![CDATA[ArrayAccess]]></code>
</MissingTemplateParam>
<MixedArgument>
<code><![CDATA[$key]]></code>
</MixedArgument>
Expand All @@ -185,13 +156,24 @@
<code><![CDATA[is_null($value) ? false : $value]]></code>
<code><![CDATA[is_null($value) ? false : $value]]></code>
</MixedReturnStatement>
<PossiblyNullArgument>
<code><![CDATA[$this->city]]></code>
<code><![CDATA[$this->state]]></code>
</PossiblyNullArgument>
<PropertyTypeCoercion>
<code><![CDATA[$this->attributes]]></code>
</PropertyTypeCoercion>
</file>
<file src="src/Services/AbstractService.php">
<MixedAssignment>
<code><![CDATA[$configValue]]></code>
</MixedAssignment>
</file>
<file src="src/Services/IPApi.php">
<MixedArgument>
<code><![CDATA[$data[0]]]></code>
<code><![CDATA[$data[0]]]></code>
<code><![CDATA[$json->countryCode]]></code>
<code><![CDATA[$path]]></code>
<code><![CDATA[$path]]></code>
<code><![CDATA[$this->config('continent_path')]]></code>
<code><![CDATA[$this->config('continent_path')]]></code>
</MixedArgument>
Expand All @@ -218,6 +200,10 @@
<MixedInferredReturnType>
<code><![CDATA[string]]></code>
</MixedInferredReturnType>
<MixedOperand>
<code><![CDATA[$json->message]]></code>
<code><![CDATA[$json->message]]></code>
</MixedOperand>
<MixedPropertyFetch>
<code><![CDATA[$json->message]]></code>
</MixedPropertyFetch>
Expand All @@ -230,14 +216,16 @@
<PropertyNotSetInConstructor>
<code><![CDATA[$continents]]></code>
</PropertyNotSetInConstructor>
<RedundantCastGivenDocblockType>
<code><![CDATA[(string) $line]]></code>
</RedundantCastGivenDocblockType>
<RedundantConditionGivenDocblockType>
<code><![CDATA[$this->client->getErrors() !== null]]></code>
<code><![CDATA[$this->client->getErrors() !== null]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Services/IPData.php">
<MixedArgument>
<code><![CDATA[$data[0]]]></code>
<code><![CDATA[$json]]></code>
<code><![CDATA[$json]]></code>
<code><![CDATA[$json]]></code>
Expand Down Expand Up @@ -277,7 +265,6 @@
</file>
<file src="src/Services/IPFinder.php">
<MixedArgument>
<code><![CDATA[$data[0]]]></code>
<code><![CDATA[$json]]></code>
</MixedArgument>
<MixedAssignment>
Expand All @@ -289,7 +276,6 @@
</file>
<file src="src/Services/IPGeoLocation.php">
<MixedArgument>
<code><![CDATA[$data[0]]]></code>
<code><![CDATA[$json]]></code>
</MixedArgument>
<MixedAssignment>
Expand All @@ -314,6 +300,7 @@
<code><![CDATA[$lang]]></code>
<code><![CDATA[$lang]]></code>
<code><![CDATA[$this->config('database_path')]]></code>
<code><![CDATA[$this->config('database_path')]]></code>
<code><![CDATA[$this->config('locales', ['en'])]]></code>
<code><![CDATA[$this->config('update_url')]]></code>
</MixedArgument>
Expand Down Expand Up @@ -342,7 +329,6 @@
<code><![CDATA[$lang]]></code>
<code><![CDATA[$this->config('license_key')]]></code>
<code><![CDATA[$this->config('locales', ['en'])]]></code>
<code><![CDATA[$this->config('user_id')]]></code>
</MixedArgument>
<MixedArrayOffset>
<code><![CDATA[$localizations[$lang]]]></code>
Expand Down
29 changes: 29 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withSets([
\Rector\Set\ValueObject\LevelSetList::UP_TO_PHP_81,
\Rector\Set\ValueObject\SetList::CODE_QUALITY,
\Rector\Set\ValueObject\SetList::CODING_STYLE,
\Rector\Set\ValueObject\SetList::TYPE_DECLARATION,
\Rector\Set\ValueObject\SetList::PRIVATIZATION,
\Rector\PHPUnit\Set\PHPUnitSetList::PHPUNIT_CODE_QUALITY,
\Rector\PHPUnit\Set\PHPUnitSetList::PHPUNIT_100,
])
->withRules([
\Rector\Php55\Rector\String_\StringClassNameToClassConstantRector::class,
\Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector::class,
\Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector::class,
\Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector::class,
\Rector\CodingStyle\Rector\Closure\StaticClosureRector::class,

])
->withParallel();
18 changes: 5 additions & 13 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@ class Cache
/**
* Instance of cache manager.
*
* @var \Illuminate\Cache\CacheManager
* @var \Illuminate\Cache\CacheManager|\Illuminate\Cache\TaggedCache
*/
protected $cache;

/**
* Lifetime of the cache.
*
* @var int
*/
protected $expires;

/** Cache prefix */
protected string $prefix = '';

Expand All @@ -35,10 +28,9 @@ class Cache
* @param array $tags
* @param int $expires
*/
public function __construct(CacheManager $cache, $tags, $expires = 30)
public function __construct(CacheManager $cache, $tags, protected int $expires = 30)
{
$this->cache = ($tags === [] || !$cache->supportsTags()) ? $cache : $cache->tags($tags);
$this->expires = $expires;
}

/**
Expand All @@ -57,7 +49,7 @@ public function setPrefix(?string $prefix = null): void
*
* @return Location|null
*/
public function get($name)
public function get(string $name): ?\InteractionDesignFoundation\GeoIP\Location
{
/** @psalm-var LocationArray|null $value */
$value = $this->cache->get($this->prefix . $name);
Expand All @@ -75,7 +67,7 @@ public function get($name)
*
* @return bool
*/
public function set($name, Location $location)
public function set(string $name, Location $location)
{
return $this->cache->put($this->prefix . $name, $location->toArray(), $this->expires);
}
Expand All @@ -85,7 +77,7 @@ public function set($name, Location $location)
*
* @return bool
*/
public function flush()
public function flush(): bool
{
return $this->cache->flush();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Clear.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public function handle(): int
*
* @return bool
*/
protected function isSupported()
protected function isSupported(): bool
{
return empty(app('geoip')->config('cache_tags')) === false
&& in_array(config('cache.default'), ['file', 'database'], true) === false;
return (empty(app('geoip')->config('cache_tags')) === false)
&& (in_array(config('cache.default'), ['file', 'database'], true) === false);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Console/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ public function handle(): int
{
// Get default service
try {
/** @var \InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface $service */
$service = app('geoip')->getService();
} catch (MissingConfigurationException $e) {
$this->components->error($e->getMessage());
} catch (MissingConfigurationException $missingConfigurationException) {
$this->components->error($missingConfigurationException->getMessage());

return static::FAILURE;
}

// Ensure the selected service supports updating
if (method_exists($service, 'update') === false) {
$this->info('The current service "' . get_class($service) . '" does not support updating.');
$this->info('The current service "' . $service::class . '" does not support updating.');

return static::SUCCESS;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Contracts/ServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public function hydrate(array $attributes = []);
* Get configuration value.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function config($key, $default = null);
public function config($key, mixed $default = null);
}
2 changes: 1 addition & 1 deletion src/Facades/GeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @method static string getClientIP()
* @method static string getCurrency(string $iso)
* @method static \InteractionDesignFoundation\GeoIP\Contracts\ServiceInterface getService()
* @mixin \InteractionDesignFoundation\GeoIP\GeoIP
* @see \InteractionDesignFoundation\GeoIP\GeoIP
*/
class GeoIP extends Facade
{
Expand Down
Loading

0 comments on commit bfe60b6

Please sign in to comment.