Add phpstan

This commit is contained in:
Michael Weimann 2022-12-04 14:08:06 +01:00 committed by Igor Scheller
parent 0e3d47f2e1
commit 9396a49412
10 changed files with 203 additions and 64 deletions

View File

@ -12,6 +12,7 @@ variables:
DOCROOT: /var/www/
stages:
- prepare
- validate
- build
- test
@ -20,56 +21,90 @@ stages:
- deploy-production
- stop
#
# Validation
#
.use_cache: &use_cache
cache:
key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
paths:
- .yarn-cache/
- vendor/
check-php-style:
image: composer:latest
stage: validate
before_script:
- composer --no-ansi global config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
- composer --no-ansi global require slevomat/coding-standard squizlabs/php_codesniffer
- export PATH=$PATH:$COMPOSER_HOME/vendor/bin
script:
- phpcs -p --no-colors --basepath="$PWD"
# does everything that depends on packages installed by yarn
check-frontend:
image: node:alpine
stage: validate
# for jobs that depend on composer
.use_composer: &use_composer
<<: *use_cache
needs:
- validate-yarn
- composer install
before_script:
- yarn --frozen-lockfile
script:
- yarn check
- yarn lint
- composer install --no-ansi --no-progress
check-editorconfig:
image: mstruebing/editorconfig-checker
stage: validate
script:
- ec -v
# for jobs that depend on yarn
.use_yarn: &use_yarn
<<: *use_cache
needs:
- yarn install
before_script:
- yarn install --check-frontend --cache-folder .yarn-cache
validate-composer:
#
# Preparation
#
composer validate:
image: composer:latest
stage: validate
stage: prepare
script:
- composer --no-ansi validate --strict
validate-yarn:
composer install:
<<: *use_cache
image: composer:latest
stage: prepare
needs:
- composer audit
- composer validate
script:
- composer install --no-ansi --no-progress
composer audit:
image: php:latest
stage: prepare
needs:
- composer validate
before_script:
- curl -Ls https://github.com/symfony/cli/releases/latest/download/symfony_linux_amd64.gz | gzip -d > /bin/symfony
- chmod +x /bin/symfony
script:
- symfony check:security --no-ansi
yarn-validate:
image: node:alpine
stage: validate
stage: prepare
before_script:
- yarn global add package-json-validator
- export PATH=$PATH:~/.yarn/bin
script:
- pjv
yarn install:
<<: *use_cache
image: node:alpine
stage: prepare
needs:
- yarn-validate
- yarn audit
script:
- yarn install --check-frontend --cache-folder .yarn-cache
yarn audit:
image: node:alpine
stage: prepare
needs:
- yarn-validate
script:
- yarn audit
generate-version:
image: alpine
stage: validate
stage: prepare
artifacts:
name: "${CI_JOB_NAME}_${CI_JOB_ID}_version"
expire_in: 1 day
@ -82,6 +117,44 @@ generate-version:
- echo "${VERSION}"
- echo -n "${VERSION}" > storage/app/VERSION
#
# Validation
#
phpcs:
<<: *use_composer
image: composer:latest
stage: validate
script:
- ./vendor/bin/phpcs -p --no-colors --basepath="$PWD"
phpstan:
<<: *use_composer
image: composer:latest
stage: validate
script:
- ./vendor/bin/phpstan --no-progress
yarn check:
<<: *use_yarn
image: node:alpine
stage: validate
script:
- yarn check
eslint:
<<: *use_yarn
image: node:alpine
stage: validate
script:
- yarn lint
check-editorconfig:
image: mstruebing/editorconfig-checker
stage: validate
script:
- ec -v
#
# Build
#
@ -100,11 +173,12 @@ build-image:
stage: build
needs:
- check-editorconfig
- check-php-style
- check-frontend
- phpcs
- phpstan
- composer validate
- yarn check
- eslint
- generate-version
- validate-composer
- validate-yarn
dependencies:
- generate-version
script:
@ -116,23 +190,6 @@ build-image:
# Test
#
audit-composer:
image: php:latest
stage: test
needs: [ ]
before_script:
- curl -Ls https://github.com/symfony/cli/releases/latest/download/symfony_linux_amd64.gz | gzip -d > /bin/symfony
- chmod +x /bin/symfony
script:
- symfony check:security --no-ansi
audit-yarn:
image: node:alpine
stage: test
needs: [ ]
script:
- yarn audit
test:
image:
name: ${TEST_IMAGE}
@ -231,8 +288,8 @@ build-release-file:
stage: release
needs:
- build-image
- audit-yarn
- audit-composer
- yarn audit
- composer audit
- test
- dump-database
- generate-assets
@ -394,8 +451,8 @@ deploy-production:
stage: deploy-production
needs:
- test
- audit-yarn
- audit-composer
- yarn audit
- composer audit
- build-image
- generate-assets
dependencies:
@ -422,8 +479,8 @@ deploy-k8s-production:
stage: deploy-production
needs:
- release-image
- audit-yarn
- audit-composer
- yarn audit
- composer audit
environment:
name: production
on_stop: stop-k8s-production

View File

@ -4,6 +4,7 @@
You may use `composer run phpcs` and [Editorconfig-Checker](https://editorconfig-checker.github.io) to verify that.
* You should use an [editorconfig plugin for your editor](https://editorconfig.org/#pre-installed) for automatic basic code formatting.
* Use `use` statements wherever possible instead of writing the fully qualified name.
* Code must pass PHPStan checks (`composer phpstan`)
* Order the composer/npm dependencies alphabetically.
* Do not use code from the [includes](includes) directory anywhere else.
* Please cover your code by unit tests, our goal is to stay at 100% line coverage.

View File

@ -108,6 +108,14 @@ PRODUCTION_REMOTE # Same as STAGING_REMOTE but for the production environm
PRODUCTION_REMOTE_PATH # Same as STAGING_REMOTE_PATH but for the production environment
```
## Static code analysis
You can run a static code analysis with this command:
```bash
composer phpstan
```
## Docker
If unspecific issues appear try using Docker version >= 20.10.14.

View File

@ -15,7 +15,8 @@
],
"scripts": {
"phpcs": "phpcs -p",
"phpcbf": "phpcbf -p"
"phpcbf": "phpcbf -p",
"phpstan": "phpstan"
},
"require": {
"php": ">=7.4.0",
@ -51,6 +52,7 @@
"dms/phpunit-arraysubset-asserts": "^0.3.1",
"fakerphp/faker": "^1.17",
"filp/whoops": "^2.14",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5",
"slevomat/coding-standard": "^7.1",
"squizlabs/php_codesniffer": "^3.6",

61
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b2eb1187f2e6d351073c1f8cdde0495d",
"content-hash": "753487aad9b4365e8f96952f4fb98d00",
"packages": [
{
"name": "composer/package-versions-deprecated",
@ -5854,6 +5854,65 @@
},
"time": "2022-04-14T12:24:06+00:00"
},
{
"name": "phpstan/phpstan",
"version": "1.9.2",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "d6fdf01c53978b6429f1393ba4afeca39cc68afa"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/d6fdf01c53978b6429f1393ba4afeca39cc68afa",
"reference": "d6fdf01c53978b6429f1393ba4afeca39cc68afa",
"shasum": ""
},
"require": {
"php": "^7.2|^8.0"
},
"conflict": {
"phpstan/phpstan-shim": "*"
},
"bin": [
"phpstan",
"phpstan.phar"
],
"type": "library",
"autoload": {
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "PHPStan - PHP Static Analysis Tool",
"keywords": [
"dev",
"static analysis"
],
"support": {
"issues": "https://github.com/phpstan/phpstan/issues",
"source": "https://github.com/phpstan/phpstan/tree/1.9.2"
},
"funding": [
{
"url": "https://github.com/ondrejmirtes",
"type": "github"
},
{
"url": "https://github.com/phpstan",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan",
"type": "tidelift"
}
],
"time": "2022-11-10T09:56:11+00:00"
},
{
"name": "phpunit/php-code-coverage",
"version": "9.2.10",

10
phpstan.neon.dist Normal file
View File

@ -0,0 +1,10 @@
parameters:
phpVersion: 70400
level: 1
paths:
- config
- db
- src
- tests
scanDirectories:
- includes

View File

@ -82,7 +82,7 @@ class LegacyDevelopment extends Legacy
$return[] = [
'file' => $path . ':' . $line,
$functionName => $args ?? null,
$functionName => $args,
];
}

View File

@ -45,7 +45,7 @@ class MailerServiceProvider extends ServiceProvider
}
/**
* @param string $transport
* @param string|null $transport
* @param array $config
* @return TransportInterface
*/

View File

@ -9,6 +9,8 @@ use Engelsystem\Test\Unit\TestCase;
class ChecksArrivalsAndDeparturesTest extends TestCase
{
private Config $config;
public function invalidArrivalCombinations(): array
{
return [

View File

@ -41,7 +41,7 @@ class ControllerTest extends TestCase
$engine->expects($this->once())
->method('get')
->willReturnCallback(function ($path, $data) use ($response) {
->willReturnCallback(function ($path, $data) {
$this->assertEquals('/metrics', $path);
$this->assertArrayHasKey('info', $data);
$this->assertArrayHasKey('users', $data);