Skip to content

Commit 5127b14

Browse files
committed
[FEATURE] Add phpstan as quality tool
- add to dev requirements - add suggested extensions to dev environment - add phpstan test to github pipeline - remove some phpstan errors for level 0 Related: #1103
1 parent b8b3f56 commit 5127b14

File tree

9 files changed

+4675
-53
lines changed

9 files changed

+4675
-53
lines changed

.github/workflows/testing.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ jobs:
5555
run: "composer install"
5656
- name: "Run PHP CS Fixer"
5757
run: "composer test:php:cs"
58+
phpstan:
59+
name: "PHPstan"
60+
runs-on: ubuntu-20.04
61+
needs: php-lint
62+
steps:
63+
- name: "Checkout"
64+
uses: actions/checkout@v4
65+
- name: "Install PHP"
66+
uses: shivammathur/setup-php@v2
67+
with:
68+
php-version: 8.1
69+
- name: "Composer Install"
70+
run: "composer install"
71+
- name: "Run PHPstan"
72+
run: "composer test:php:phpstan"
5873
unit-tests:
5974
name: "PHP Unit Tests"
6075
runs-on: ubuntu-20.04

.project/tests/phpstan-baseline.neon

Lines changed: 4606 additions & 0 deletions
Large diffs are not rendered by default.

.project/tests/phpstan.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
parameters:
4+
level: 8
5+
paths:
6+
- ../../Classes
7+
8+
# ToDo: fix ../../Tests for inclusion in phpstan

.project/typo3/settings.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
'scheduler' => [
5959
'maxLifetime' => '1440',
6060
],
61+
'static_info_tables' => [
62+
'enableManager' => '0',
63+
],
6164
],
6265
'FE' => [
6366
'debug' => true,

Classes/Controller/ModuleController.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function initializeAction(): void
7171
}
7272

7373
/**
74-
* @return void
74+
* @return ResponseInterface
7575
* @noinspection PhpUnused
7676
*/
7777
public function dispatchAction(string $forwardToAction = 'list'): ResponseInterface
@@ -130,11 +130,11 @@ public function listAction(): ResponseInterface
130130
}
131131

132132
/**
133-
* @return void
133+
* @return ResponseInterface|null
134134
* @throws InvalidQueryException
135135
* @noinspection PhpUnused
136136
*/
137-
public function exportXlsAction(): ResponseInterface
137+
public function exportXlsAction(): ?ResponseInterface
138138
{
139139
if ($this->isPhpSpreadsheetInstalled) {
140140
$this->view->assignMultiple(
@@ -164,10 +164,11 @@ public function exportXlsAction(): ResponseInterface
164164
->withAddedHeader('Pragma', 'no-cache')
165165
->withBody($this->streamFactory->createStreamFromFile($tmpFilename));
166166
}
167+
return null;
167168
}
168169

169170
/**
170-
* @return void
171+
* @return ResponseInterface
171172
* @throws InvalidQueryException
172173
* @noinspection PhpUnused
173174
*/
@@ -193,7 +194,7 @@ public function exportCsvAction(): ResponseInterface
193194
}
194195

195196
/**
196-
* @return void
197+
* @return ResponseInterface
197198
* @throws InvalidQueryException
198199
* @throws RouteNotFoundException
199200
* @noinspection PhpUnused
@@ -219,7 +220,7 @@ public function reportingFormBeAction(): ResponseInterface
219220
}
220221

221222
/**
222-
* @return void
223+
* @return ResponseInterface
223224
* @throws InvalidQueryException
224225
* @throws RouteNotFoundException
225226
* @throws PropertyNotAccessibleException
@@ -246,7 +247,7 @@ public function reportingMarketingBeAction(): ResponseInterface
246247
}
247248

248249
/**
249-
* @return void
250+
* @return ResponseInterface
250251
* @throws InvalidQueryException
251252
* @throws ExceptionExtbaseObject
252253
* @noinspection PhpUnused

Classes/Domain/Validator/CaptchaValidator.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
use In2code\Powermail\Domain\Service\CalculatingCaptchaService;
1212
use In2code\Powermail\Exception\DeprecatedException;
1313
use In2code\Powermail\Utility\TypoScriptUtility;
14-
use ThinkopenAt\Captcha\Utility;
1514
use TYPO3\CMS\Core\Package\Exception as ExceptionCore;
16-
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
1715
use TYPO3\CMS\Core\Utility\GeneralUtility;
18-
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
1916
use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
2017

2118
/**
@@ -85,10 +82,6 @@ public function isValid($mail): void
8582
protected function validCodePreflight(string $value, Field $field): bool
8683
{
8784
switch (TypoScriptUtility::getCaptchaExtensionFromSettings($this->settings)) {
88-
case 'captcha':
89-
$result = $this->validateCaptcha($value, $field);
90-
break;
91-
9285
default:
9386
$result = $this->validatePowermailCaptcha($value, $field);
9487
}
@@ -106,21 +99,6 @@ protected function validatePowermailCaptcha(string $value, Field $field): bool
10699
return $captchaService->validCode($value, $field, $this->isClearSession());
107100
}
108101

109-
/**
110-
* @param string $value
111-
* @param Field $field
112-
* @return bool
113-
* @throws ExceptionCore
114-
*/
115-
protected function validateCaptcha(string $value, Field $field): bool
116-
{
117-
$captchaVersion = ExtensionManagementUtility::getExtensionVersion('captcha');
118-
if (VersionNumberUtility::convertVersionNumberToInteger($captchaVersion) >= 2000000) {
119-
return Utility::checkCaptcha($value, $field->getUid());
120-
}
121-
return $this->validateCaptchaOld($value);
122-
}
123-
124102
/**
125103
* @param string $value
126104
* @return bool

Classes/ViewHelpers/Validation/CaptchaViewHelper.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@
77
use In2code\Powermail\Domain\Model\Field;
88
use In2code\Powermail\Domain\Service\CalculatingCaptchaService;
99
use In2code\Powermail\Domain\Service\ConfigurationService;
10-
use In2code\Powermail\Utility\BasicFileUtility;
11-
use In2code\Powermail\Utility\StringUtility;
12-
use In2code\Powermail\Utility\TypoScriptUtility;
13-
use ThinkopenAt\Captcha\Utility;
1410
use TYPO3\CMS\Core\Package\Exception as ExceptionCore;
15-
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
1611
use TYPO3\CMS\Core\Utility\GeneralUtility;
17-
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
1812
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
1913

2014
/**
@@ -89,23 +83,8 @@ protected function getErrorMessage(Exception $exception): string
8983
*/
9084
protected function getImageSource(Field $field): string
9185
{
92-
$settings = $this->getSettings();
93-
switch (TypoScriptUtility::getCaptchaExtensionFromSettings($settings)) {
94-
case 'captcha':
95-
$captchaVersion = ExtensionManagementUtility::getExtensionVersion('captcha');
96-
$image = BasicFileUtility::getRelativeFolder(ExtensionManagementUtility::extPath('captcha'))
97-
. 'captcha/captcha.php';
98-
if (VersionNumberUtility::convertVersionNumberToInteger($captchaVersion) >= 2000000) {
99-
$imageTag = Utility::makeCaptcha($field->getUid());
100-
return StringUtility::getSrcFromImageTag($imageTag);
101-
}
102-
break;
103-
104-
default:
105-
$captchaService = GeneralUtility::makeInstance(CalculatingCaptchaService::class);
106-
$image = $captchaService->render($field);
107-
}
108-
return $image;
86+
$captchaService = GeneralUtility::makeInstance(CalculatingCaptchaService::class);
87+
return $captchaService->render($field);
10988
}
11089

11190
/**

Documentation/ForContributors/Readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ Backend: https://powermail-<TYPO3-version>.ddev.site/typo3
2424
Username: admin \
2525
Password: password
2626

27+
## PHP tests
28+
29+
There are several test types preconfigured in EXT:powermail. These are
30+
31+
- phplint
32+
- php-cs-fixer
33+
- phpstan
34+
- php unit test
35+
36+
All can be triggered locally via `composer`.
37+
38+
```bash
39+
ddev exec composer run test:php:phpstan
40+
```
41+
42+
### PHPstan: Update baseline
43+
44+
As the time of this writing (while introducing phpstan in Sept. 2024), there are slightly over 1000 issues in the
45+
phpstan base. (Hopefully) They will be reduced, in future development. If you fixed one or more of them, it will be
46+
reported in the github pipeline or locally. If done so, they must be removed from the baseline with the following
47+
command.
48+
49+
```bash
50+
ddev exec composer run test:php:phpstan:generate-baseline
51+
```
52+
53+
54+
2755
## Behaviour tests
2856

2957
More information on running behaviour tests is available here: [Behaviour tests](../../Tests/Behavior/readme.md)

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@
7878
"typo3/cms-dashboard": "^12.4",
7979
"friends-of-behat/mink-browserkit-driver": "^1.5",
8080
"drevops/behat-screenshot": "^1.1",
81-
"typo3/testing-framework": "^8.0"
81+
"typo3/testing-framework": "^8.0",
82+
"phpstan/phpstan": "^1.12.3",
83+
"jambagecom/base-excel": "^2.1"
8284
},
8385
"autoload": {
8486
"psr-4": {
@@ -106,6 +108,8 @@
106108
"test:behaviour:stop": "behat --config Tests/Behavior/behat.yml --stop-on-failure",
107109
"test:php:cs": "php-cs-fixer --version && php-cs-fixer fix --dry-run --config=.project/tests/.php-cs-fixer.php --diff",
108110
"test:php:lint": "find *.php Classes Configuration Tests -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l",
111+
"test:php:phpstan": "phpstan analyse -c .project/tests/phpstan.neon",
112+
"test:php:phpstan:generate-baseline": "phpstan analyse -c .project/tests/phpstan.neon --generate-baseline .project/tests/phpstan-baseline.neon",
109113
"test:typoscript:lint": "typoscript-lint --config=.project/tests/typoscript-lint.yml",
110114
"test:unit": "phpunit -c phpunit.xml.dist"
111115
},

0 commit comments

Comments
 (0)