Skip to content

Commit 6a4cd16

Browse files
authored
Merge pull request #349 from dotkernel/issue-324-2
Implemented PHPStan
2 parents afb2750 + b991666 commit 6a4cd16

File tree

16 files changed

+114
-55
lines changed

16 files changed

+114
-55
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
on:
2+
- push
3+
4+
name: Run PHPStan checks
5+
6+
jobs:
7+
mutation:
8+
name: PHPStan ${{ matrix.php }}-${{ matrix.os }}
9+
10+
runs-on: ${{ matrix.os }}
11+
12+
strategy:
13+
matrix:
14+
os:
15+
- ubuntu-latest
16+
17+
php:
18+
- "8.2"
19+
- "8.3"
20+
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Install PHP
26+
uses: shivammathur/setup-php@v2
27+
with:
28+
php-version: "${{ matrix.php }}"
29+
coverage: pcov
30+
ini-values: assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On
31+
tools: composer:v2, cs2pr
32+
33+
- name: Determine composer cache directory
34+
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV
35+
36+
- name: Cache dependencies installed with composer
37+
uses: actions/cache@v4
38+
with:
39+
path: ${{ env.COMPOSER_CACHE_DIR }}
40+
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
41+
restore-keys: |
42+
php${{ matrix.php }}-composer-
43+
44+
- name: Install dependencies with composer
45+
run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
46+
47+
- name: Setup project
48+
run: |
49+
mv config/autoload/local.php.dist config/autoload/local.php
50+
mv config/autoload/mail.local.php.dist config/autoload/mail.local.php
51+
mv config/autoload/local.test.php.dist config/autoload/local.test.php
52+
53+
- name: Run static analysis with PHPStan
54+
run: vendor/bin/phpstan analyse

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ Based on Enrico Zimuel's [Zend Expressive API - Skeleton example](https://github
1313
[![Build Static](https://github.com/dotkernel/api/actions/workflows/continuous-integration.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/api/actions/workflows/continuous-integration.yml)
1414
[![codecov](https://codecov.io/gh/dotkernel/api/graph/badge.svg?token=53FN78G5CK)](https://codecov.io/gh/dotkernel/api)
1515
[![Qodana](https://github.com/dotkernel/api/actions/workflows/qodana_code_quality.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/api/actions/workflows/qodana_code_quality.yml)
16-
17-
[![SymfonyInsight](https://insight.symfony.com/projects/7f9143cc-5e3c-4cfc-992c-377a001fde3e/big.svg)](https://insight.symfony.com/projects/7f9143cc-5e3c-4cfc-992c-377a001fde3e)
16+
[![PHPStan](https://github.com/dotkernel/api/actions/workflows/static-analysis.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/api/actions/workflows/static-analysis.yml)
1817

1918
## Getting Started
2019

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
"laminas/laminas-development-mode": "^3.12.0",
8383
"laminas/laminas-http": "^2.19.0",
8484
"mezzio/mezzio-tooling": "^2.9.0",
85+
"phpstan/phpstan": "^2.0",
86+
"phpstan/phpstan-doctrine": "^2.0",
87+
"phpstan/phpstan-phpunit": "^2.0",
8588
"phpunit/phpunit": "^10.5.10",
8689
"roave/security-advisories": "dev-latest",
8790
"symfony/var-dumper": "^7.1"

phpstan.neon

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
includes:
2+
- vendor/phpstan/phpstan-doctrine/extension.neon
3+
- vendor/phpstan/phpstan-phpunit/extension.neon
4+
parameters:
5+
level: 5
6+
paths:
7+
- src
8+
- test
9+
treatPhpDocTypesAsCertain: false
10+
ignoreErrors:
11+
-
12+
message: '#Call to an undefined method.*setAllowOverride#'
13+
path: test/Functional/AbstractFunctionalTest.php
14+
-
15+
message: '#Call to an undefined method.*setService#'
16+
path: test/Functional/AbstractFunctionalTest.php

src/Admin/src/Service/AdminService.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ public function deleteAdmin(Admin $admin): void
6464

6565
public function exists(string $identity = ''): bool
6666
{
67-
try {
68-
return $this->findOneBy(['identity' => $identity]) instanceof Admin;
69-
} catch (NotFoundException) {
70-
return false;
71-
}
67+
return $this->adminRepository->findOneBy(['identity' => $identity]) instanceof Admin;
7268
}
7369

7470
public function existsOther(string $identity = '', string $uuid = ''): bool

src/App/src/Entity/OAuthAuthCode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function setId(int $id): self
5757
return $this;
5858
}
5959

60-
public function getId(): ?int
60+
public function getId(): int
6161
{
6262
return $this->id;
6363
}
@@ -74,9 +74,9 @@ public function getClient(): ClientEntityInterface
7474
return $this->client;
7575
}
7676

77-
public function getIdentifier(): ?int
77+
public function getIdentifier(): string
7878
{
79-
return $this->getId();
79+
return (string) $this->getId();
8080
}
8181

8282
/**

src/App/src/Repository/OAuthClientRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getClientEntity($clientIdentifier): ?ClientEntityInterface
5252
public function validateClient($clientIdentifier, $clientSecret, $grantType): bool
5353
{
5454
$client = $this->getClientEntity($clientIdentifier);
55-
if (! $client instanceof ClientEntityInterface) {
55+
if (! $client instanceof OAuthClient) {
5656
return false;
5757
}
5858

src/App/src/UserIdentity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class UserIdentity implements UserInterface
1010
{
1111
protected string $identity;
12-
/** @var iterable<int|string, string> $roles */
12+
/** @var array<int|string, string> $roles */
1313
protected array $roles;
1414
protected array $details;
1515

test/Functional/AbstractFunctionalTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Fig\Http\Message\RequestMethodInterface;
1919
use Fig\Http\Message\StatusCodeInterface;
2020
use Laminas\Diactoros\ServerRequest;
21-
use Laminas\ServiceManager\ServiceManager;
2221
use Mezzio\Application;
2322
use Mezzio\MiddlewareFactory;
2423
use PHPUnit\Framework\TestCase;
@@ -31,7 +30,6 @@
3130

3231
use function array_merge;
3332
use function getenv;
34-
use function method_exists;
3533
use function putenv;
3634
use function realpath;
3735

@@ -41,7 +39,7 @@ class AbstractFunctionalTest extends TestCase
4139
use DatabaseTrait;
4240

4341
protected Application $app;
44-
protected ContainerInterface|ServiceManager $container;
42+
protected ContainerInterface $container;
4543
protected const DEFAULT_PASSWORD = 'dotkernel';
4644

4745
/**
@@ -59,12 +57,8 @@ public function setUp(): void
5957

6058
$this->ensureTestMode();
6159

62-
if (method_exists($this, 'runMigrations')) {
63-
$this->runMigrations();
64-
}
65-
if (method_exists($this, 'runSeeders')) {
66-
$this->runSeeders();
67-
}
60+
$this->runMigrations();
61+
$this->runSeeders();
6862
}
6963

7064
public function tearDown(): void
@@ -132,7 +126,7 @@ protected function getEntityManager(): EntityManagerInterface
132126
return $this->container->get(EntityManagerInterface::class);
133127
}
134128

135-
protected function getContainer(): ContainerInterface|ServiceManager
129+
protected function getContainer(): ContainerInterface
136130
{
137131
return $this->container;
138132
}
@@ -150,7 +144,7 @@ private function ensureTestMode(): void
150144
);
151145
}
152146

153-
if (! $this->getEntityManager()->getConnection()->getParams()['memory'] ?? false) {
147+
if (! ($this->getEntityManager()->getConnection()->getParams()['memory'] ?? false)) {
154148
throw new RuntimeException(
155149
'You are running tests in a non in-memory database. Did you forget to create local.test.php?'
156150
);
@@ -271,7 +265,7 @@ private function createRequest(
271265
string $body = 'php://input',
272266
string $protocol = '1.1'
273267
): ServerRequestInterface {
274-
if (method_exists($this, 'isAuthenticated') && $this->isAuthenticated()) {
268+
if ($this->isAuthenticated()) {
275269
$headers = array_merge($headers, $this->getAuthorizationHeader());
276270
}
277271

test/Unit/Admin/Service/AdminServiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
class AdminServiceTest extends TestCase
2222
{
2323
private Subject|MockObject $subject;
24-
private AdminRoleService $adminRoleService;
25-
private AdminRepository $adminRepository;
24+
private AdminRoleService|MockObject $adminRoleService;
25+
private AdminRepository|MockObject $adminRepository;
2626

2727
/**
2828
* @throws \PHPUnit\Framework\MockObject\Exception

0 commit comments

Comments
 (0)