Skip to content

Commit

Permalink
chore: enable phpstan, apply fixes (#31)
Browse files Browse the repository at this point in the history
* chore: enable phpstan, apply fixes

* Apply fixes from StyleCI

* remove undeeded docblock

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Nov 9, 2023
1 parent d777a58 commit ab9765f
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 60 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: FoF GeoIP PHP

on: [workflow_dispatch, push, pull_request]

jobs:
run:
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main
with:
enable_backend_testing: false
enable_phpstan: true
php_versions: '["8.0", "8.1", "8.2"]'

backend_directory: .
31 changes: 0 additions & 31 deletions .github/workflows/build.yml

This file was deleted.

19 changes: 19 additions & 0 deletions .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: FoF GeoIP JS

on: [workflow_dispatch, push, pull_request]

jobs:
run:
uses: flarum/framework/.github/workflows/REUSABLE_frontend.yml@main
with:
enable_bundlewatch: false
enable_prettier: true
enable_typescript: true

frontend_directory: ./js
backend_directory: .
js_package_manager: npm
main_git_branch: master

secrets:
bundlewatch_github_token: ${{ secrets.BUNDLEWATCH_GITHUB_TOKEN }}
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@
},
"flagrow": {
"discuss": "https://discuss.flarum.org/d/21493"
},
"flarum-cli": {
"modules": {
"githubActions": true
}
}
},
"require-dev": {
"flarum/phpstan": "*"
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
"clear-cache:phpstan": "phpstan clear-result-cache"
},
"scripts-descriptions": {
"analyse:phpstan": "Run static analysis"
}
}
13 changes: 13 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
includes:
- vendor/flarum/phpstan/extension.neon

parameters:
# The level will be increased in Flarum 2.0
level: 5
paths:
- extend.php
- src
excludePaths:
- *.blade.php
checkMissingIterableValueType: false
databaseMigrationsPath: ['migrations']
5 changes: 1 addition & 4 deletions src/Api/Controller/ShowIpInfoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ class ShowIpInfoController extends AbstractShowController
{
public $serializer = IPInfoSerializer::class;

protected $geoIP;

public function __construct(GeoIP $geoIP)
public function __construct(protected GeoIP $geoIP)
{
$this->geoIP = $geoIP;
}

/**
Expand Down
37 changes: 36 additions & 1 deletion src/Api/ServiceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ServiceResponse implements \JsonSerializable
private $threat_type;

/**
* @var
* @var ?string
*/
private $error;

Expand All @@ -65,48 +65,83 @@ public function setCountryCode(?string $country_code)
return $this;
}

public function getCountryCode(): ?string
{
return $this->country_code;
}

public function setZipCode(?string $zip_code)
{
$this->zip_code = $zip_code;

return $this;
}

public function getZipCode(): ?string
{
return $this->zip_code;
}

public function setIsp(?string $isp)
{
$this->isp = $isp;

return $this;
}

public function getIsp(): ?string
{
return $this->isp;
}

public function setOrganization(?string $organization)
{
$this->organization = $organization;

return $this;
}

public function getOrganization(): ?string
{
return $this->organization;
}

public function setThreatLevel(?string $level)
{
$this->threat_level = $level;

return $this;
}

public function getThreatLevel(): ?string
{
return $this->threat_level;
}

public function setThreatType(?string $types)
{
$this->threat_type = $types;

return $this;
}

public function getThreatType(): ?string
{
return $this->threat_type;
}

public function setError(?string $error)
{
$this->error = $error;

return $this;
}

public function getError(): ?string
{
return $this->error;
}

public function toJson()
{
return json_decode(json_encode($this), true);
Expand Down
2 changes: 2 additions & 0 deletions src/Api/Services/IPData.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ protected function parseResponse(object $body): ServiceResponse

if (isset($body->asn->type)) {
if ($body->asn->type == 'isp') {
/** @phpstan-ignore-next-line */
$response->setIsp($body->asn->name);
} else {
/** @phpstan-ignore-next-line */
$response->setOrganization($body->asn->name);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/IPInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
use Flarum\Database\AbstractModel;

/**
* @property string address
* @property string|null countryCode
* @property string|null zipCode
* @property string|null isp
* @property string|null organization
* @property string|null threat_level
* @property string|null threat_types
* @property string|null error
* @property string $address
* @property string|null $country_code
* @property string|null $zip_code
* @property string|null $isp
* @property string|null $organization
* @property string|null $threat_level
* @property string|null $threat_types
* @property string|null $error
*/
class IPInfo extends AbstractModel
{
Expand Down
22 changes: 13 additions & 9 deletions src/IPInfoSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
namespace FoF\GeoIP;

use Flarum\Api\Serializer\AbstractSerializer;
use InvalidArgumentException;

class IPInfoSerializer extends AbstractSerializer
{
/**
* {@inheritdoc}
*/
protected $type = 'ip_info';

/**
* {@inheritdoc}
* @param IPInfo $ip
*
* @return array
*/
protected function getDefaultAttributes($ip)
protected function getDefaultAttributes($ip): array
{
if (!$ip) {
return [];
if (!($ip instanceof IPInfo)) {
throw new InvalidArgumentException(
get_class($this).' can only serialize instances of '.IPInfo::class
);
}

return [
Expand All @@ -35,13 +37,15 @@ protected function getDefaultAttributes($ip)
'isp' => $ip->isp,
'organization' => $ip->organization,
'threatLevel' => $ip->threat_level,
'threatType' => $ip->threat_type,
'threatType' => $ip->threat_types,
'error' => $ip->error,
];
}

/**
* {@inheritdoc}
* @param IPInfo $model
*
* @return string
*/
public function getId($model)
{
Expand Down
8 changes: 1 addition & 7 deletions src/Listeners/RemoveErrorsOnSettingsUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@

class RemoveErrorsOnSettingsUpdate
{
/**
* @var SettingsRepositoryInterface
*/
private $settings;

public function __construct(SettingsRepositoryInterface $settings)
public function __construct(protected SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}

public function handle(Saving $event)
Expand Down

0 comments on commit ab9765f

Please sign in to comment.