diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml new file mode 100644 index 0000000..b82b517 --- /dev/null +++ b/.github/workflows/backend.yml @@ -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: . diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 91f877b..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: JavaScript - -on: - push: - branches: - - master - -jobs: - build: - name: JS / Build - runs-on: ubuntu-latest - - steps: - - name: Check out code - uses: actions/checkout@v3 - - - name: Restore npm cache - uses: actions/cache@v3 - with: - path: ~/.npm - key: ${{ runner.os }}-node-${{ hashFiles('js/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - # Our action will install npm, cd into `./js`, run `npm run build` and - # `npm run build-typings`, then commit and upload any changes - - name: Build production JS - uses: flarum/action-build@v3 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - build_script: build - package_manager: npm diff --git a/.github/workflows/frontend.yml b/.github/workflows/frontend.yml new file mode 100644 index 0000000..bfa0c58 --- /dev/null +++ b/.github/workflows/frontend.yml @@ -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 }} diff --git a/composer.json b/composer.json index 6bb6bcc..50bbcb5 100644 --- a/composer.json +++ b/composer.json @@ -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" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..03cf261 --- /dev/null +++ b/phpstan.neon @@ -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'] diff --git a/src/Api/Controller/ShowIpInfoController.php b/src/Api/Controller/ShowIpInfoController.php index e62e13b..895904c 100644 --- a/src/Api/Controller/ShowIpInfoController.php +++ b/src/Api/Controller/ShowIpInfoController.php @@ -25,12 +25,8 @@ class ShowIpInfoController extends AbstractShowController { public $serializer = IPInfoSerializer::class; - protected $geoIP; - - public function __construct(GeoIP $geoIP) - { - $this->geoIP = $geoIP; - } + public function __construct(protected GeoIP $geoIP) + {} /** * Get the IP information, either from the database or by performing a lookup. diff --git a/src/Api/ServiceResponse.php b/src/Api/ServiceResponse.php index d30a48f..b4a625f 100644 --- a/src/Api/ServiceResponse.php +++ b/src/Api/ServiceResponse.php @@ -49,7 +49,7 @@ class ServiceResponse implements \JsonSerializable private $threat_type; /** - * @var + * @var ?string */ private $error; @@ -65,6 +65,11 @@ 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; @@ -72,6 +77,11 @@ public function setZipCode(?string $zip_code) return $this; } + public function getZipCode(): ?string + { + return $this->zip_code; + } + public function setIsp(?string $isp) { $this->isp = $isp; @@ -79,6 +89,11 @@ public function setIsp(?string $isp) return $this; } + public function getIsp(): ?string + { + return $this->isp; + } + public function setOrganization(?string $organization) { $this->organization = $organization; @@ -86,6 +101,11 @@ public function setOrganization(?string $organization) return $this; } + public function getOrganization(): ?string + { + return $this->organization; + } + public function setThreatLevel(?string $level) { $this->threat_level = $level; @@ -93,6 +113,11 @@ public function setThreatLevel(?string $level) return $this; } + public function getThreatLevel(): ?string + { + return $this->threat_level; + } + public function setThreatType(?string $types) { $this->threat_type = $types; @@ -100,6 +125,11 @@ public function setThreatType(?string $types) return $this; } + public function getThreatType(): ?string + { + return $this->threat_type; + } + public function setError(?string $error) { $this->error = $error; @@ -107,6 +137,11 @@ public function setError(?string $error) return $this; } + public function getError(): ?string + { + return $this->error; + } + public function toJson() { return json_decode(json_encode($this), true); diff --git a/src/Api/Services/IPData.php b/src/Api/Services/IPData.php index cbdacf6..f76b1df 100644 --- a/src/Api/Services/IPData.php +++ b/src/Api/Services/IPData.php @@ -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); } } diff --git a/src/IPInfo.php b/src/IPInfo.php index ac9c034..5726951 100644 --- a/src/IPInfo.php +++ b/src/IPInfo.php @@ -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 { diff --git a/src/IPInfoSerializer.php b/src/IPInfoSerializer.php index 22c8d7b..09f8aae 100644 --- a/src/IPInfoSerializer.php +++ b/src/IPInfoSerializer.php @@ -12,21 +12,24 @@ namespace FoF\GeoIP; use Flarum\Api\Serializer\AbstractSerializer; +use InvalidArgumentException; class IPInfoSerializer extends AbstractSerializer { - /** - * {@inheritdoc} - */ protected $type = 'ip_info'; /** - * {@inheritdoc} + * Undocumented function + * + * @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 [ @@ -35,13 +38,14 @@ 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) { diff --git a/src/Listeners/RemoveErrorsOnSettingsUpdate.php b/src/Listeners/RemoveErrorsOnSettingsUpdate.php index bb33d8a..284ec4b 100644 --- a/src/Listeners/RemoveErrorsOnSettingsUpdate.php +++ b/src/Listeners/RemoveErrorsOnSettingsUpdate.php @@ -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)