Skip to content

Commit

Permalink
[FEATURE] Add phpstan as quality tool
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mschwemer committed Sep 18, 2024
1 parent b8b3f56 commit 5127b14
Show file tree
Hide file tree
Showing 9 changed files with 4,675 additions and 53 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ jobs:
run: "composer install"
- name: "Run PHP CS Fixer"
run: "composer test:php:cs"
phpstan:
name: "PHPstan"
runs-on: ubuntu-20.04
needs: php-lint
steps:
- name: "Checkout"
uses: actions/checkout@v4
- name: "Install PHP"
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
- name: "Composer Install"
run: "composer install"
- name: "Run PHPstan"
run: "composer test:php:phpstan"
unit-tests:
name: "PHP Unit Tests"
runs-on: ubuntu-20.04
Expand Down
4,606 changes: 4,606 additions & 0 deletions .project/tests/phpstan-baseline.neon

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions .project/tests/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
includes:
- phpstan-baseline.neon
parameters:
level: 8
paths:
- ../../Classes

# ToDo: fix ../../Tests for inclusion in phpstan
3 changes: 3 additions & 0 deletions .project/typo3/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
'scheduler' => [
'maxLifetime' => '1440',
],
'static_info_tables' => [
'enableManager' => '0',
],
],
'FE' => [
'debug' => true,
Expand Down
15 changes: 8 additions & 7 deletions Classes/Controller/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function initializeAction(): void
}

/**
* @return void
* @return ResponseInterface
* @noinspection PhpUnused
*/
public function dispatchAction(string $forwardToAction = 'list'): ResponseInterface
Expand Down Expand Up @@ -130,11 +130,11 @@ public function listAction(): ResponseInterface
}

/**
* @return void
* @return ResponseInterface|null
* @throws InvalidQueryException
* @noinspection PhpUnused
*/
public function exportXlsAction(): ResponseInterface
public function exportXlsAction(): ?ResponseInterface
{
if ($this->isPhpSpreadsheetInstalled) {
$this->view->assignMultiple(
Expand Down Expand Up @@ -164,10 +164,11 @@ public function exportXlsAction(): ResponseInterface
->withAddedHeader('Pragma', 'no-cache')
->withBody($this->streamFactory->createStreamFromFile($tmpFilename));
}
return null;
}

/**
* @return void
* @return ResponseInterface
* @throws InvalidQueryException
* @noinspection PhpUnused
*/
Expand All @@ -193,7 +194,7 @@ public function exportCsvAction(): ResponseInterface
}

/**
* @return void
* @return ResponseInterface
* @throws InvalidQueryException
* @throws RouteNotFoundException
* @noinspection PhpUnused
Expand All @@ -219,7 +220,7 @@ public function reportingFormBeAction(): ResponseInterface
}

/**
* @return void
* @return ResponseInterface
* @throws InvalidQueryException
* @throws RouteNotFoundException
* @throws PropertyNotAccessibleException
Expand All @@ -246,7 +247,7 @@ public function reportingMarketingBeAction(): ResponseInterface
}

/**
* @return void
* @return ResponseInterface
* @throws InvalidQueryException
* @throws ExceptionExtbaseObject
* @noinspection PhpUnused
Expand Down
22 changes: 0 additions & 22 deletions Classes/Domain/Validator/CaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
use In2code\Powermail\Domain\Service\CalculatingCaptchaService;
use In2code\Powermail\Exception\DeprecatedException;
use In2code\Powermail\Utility\TypoScriptUtility;
use ThinkopenAt\Captcha\Utility;
use TYPO3\CMS\Core\Package\Exception as ExceptionCore;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;

/**
Expand Down Expand Up @@ -85,10 +82,6 @@ public function isValid($mail): void
protected function validCodePreflight(string $value, Field $field): bool
{
switch (TypoScriptUtility::getCaptchaExtensionFromSettings($this->settings)) {
case 'captcha':
$result = $this->validateCaptcha($value, $field);
break;

default:
$result = $this->validatePowermailCaptcha($value, $field);
}
Expand All @@ -106,21 +99,6 @@ protected function validatePowermailCaptcha(string $value, Field $field): bool
return $captchaService->validCode($value, $field, $this->isClearSession());
}

/**
* @param string $value
* @param Field $field
* @return bool
* @throws ExceptionCore
*/
protected function validateCaptcha(string $value, Field $field): bool
{
$captchaVersion = ExtensionManagementUtility::getExtensionVersion('captcha');
if (VersionNumberUtility::convertVersionNumberToInteger($captchaVersion) >= 2000000) {
return Utility::checkCaptcha($value, $field->getUid());
}
return $this->validateCaptchaOld($value);
}

/**
* @param string $value
* @return bool
Expand Down
25 changes: 2 additions & 23 deletions Classes/ViewHelpers/Validation/CaptchaViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@
use In2code\Powermail\Domain\Model\Field;
use In2code\Powermail\Domain\Service\CalculatingCaptchaService;
use In2code\Powermail\Domain\Service\ConfigurationService;
use In2code\Powermail\Utility\BasicFileUtility;
use In2code\Powermail\Utility\StringUtility;
use In2code\Powermail\Utility\TypoScriptUtility;
use ThinkopenAt\Captcha\Utility;
use TYPO3\CMS\Core\Package\Exception as ExceptionCore;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;

/**
Expand Down Expand Up @@ -89,23 +83,8 @@ protected function getErrorMessage(Exception $exception): string
*/
protected function getImageSource(Field $field): string
{
$settings = $this->getSettings();
switch (TypoScriptUtility::getCaptchaExtensionFromSettings($settings)) {
case 'captcha':
$captchaVersion = ExtensionManagementUtility::getExtensionVersion('captcha');
$image = BasicFileUtility::getRelativeFolder(ExtensionManagementUtility::extPath('captcha'))
. 'captcha/captcha.php';
if (VersionNumberUtility::convertVersionNumberToInteger($captchaVersion) >= 2000000) {
$imageTag = Utility::makeCaptcha($field->getUid());
return StringUtility::getSrcFromImageTag($imageTag);
}
break;

default:
$captchaService = GeneralUtility::makeInstance(CalculatingCaptchaService::class);
$image = $captchaService->render($field);
}
return $image;
$captchaService = GeneralUtility::makeInstance(CalculatingCaptchaService::class);
return $captchaService->render($field);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions Documentation/ForContributors/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ Backend: https://powermail-<TYPO3-version>.ddev.site/typo3
Username: admin \
Password: password

## PHP tests

There are several test types preconfigured in EXT:powermail. These are

- phplint
- php-cs-fixer
- phpstan
- php unit test

All can be triggered locally via `composer`.

```bash
ddev exec composer run test:php:phpstan
```

### PHPstan: Update baseline

As the time of this writing (while introducing phpstan in Sept. 2024), there are slightly over 1000 issues in the
phpstan base. (Hopefully) They will be reduced, in future development. If you fixed one or more of them, it will be
reported in the github pipeline or locally. If done so, they must be removed from the baseline with the following
command.

```bash
ddev exec composer run test:php:phpstan:generate-baseline
```



## Behaviour tests

More information on running behaviour tests is available here: [Behaviour tests](../../Tests/Behavior/readme.md)
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
"typo3/cms-dashboard": "^12.4",
"friends-of-behat/mink-browserkit-driver": "^1.5",
"drevops/behat-screenshot": "^1.1",
"typo3/testing-framework": "^8.0"
"typo3/testing-framework": "^8.0",
"phpstan/phpstan": "^1.12.3",
"jambagecom/base-excel": "^2.1"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -106,6 +108,8 @@
"test:behaviour:stop": "behat --config Tests/Behavior/behat.yml --stop-on-failure",
"test:php:cs": "php-cs-fixer --version && php-cs-fixer fix --dry-run --config=.project/tests/.php-cs-fixer.php --diff",
"test:php:lint": "find *.php Classes Configuration Tests -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l",
"test:php:phpstan": "phpstan analyse -c .project/tests/phpstan.neon",
"test:php:phpstan:generate-baseline": "phpstan analyse -c .project/tests/phpstan.neon --generate-baseline .project/tests/phpstan-baseline.neon",
"test:typoscript:lint": "typoscript-lint --config=.project/tests/typoscript-lint.yml",
"test:unit": "phpunit -c phpunit.xml.dist"
},
Expand Down

0 comments on commit 5127b14

Please sign in to comment.