Add PHPStan to ensure code quality and prevent bugs

This commit is contained in:
Daniel Siepmann 2020-04-01 20:40:00 +02:00
parent e06cc3c5c5
commit 669d75ce86
10 changed files with 49 additions and 24 deletions

View file

@ -15,5 +15,8 @@ jobs:
- name: Test CGL
run: ./vendor/bin/phpcs
- name: Execute PHPStan Code Quality
run: ./vendor/bin/phpstan analyse
- name: Execute PHPUnit Tests
run: ./vendor/bin/phpunit

View file

@ -110,10 +110,15 @@ class PageViewsBar extends AbstractBarChartWidget
$format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
for ($daysBefore = $days; $daysBefore >= 0; $daysBefore--) {
$labels[] = date($format, strtotime('-' . $daysBefore . ' day'));
$timeForLabel = strtotime('-' . $daysBefore . ' day');
$startPeriod = strtotime('-' . $daysBefore . ' day 0:00:00');
$endPeriod = strtotime('-' . $daysBefore . ' day 23:59:59');
if ($timeForLabel === false || $startPeriod === false || $endPeriod === false) {
continue;
}
$labels[] = date($format, $timeForLabel);
$data[] = $this->getPageViewsInPeriod($startPeriod, $endPeriod);
}

View file

@ -25,6 +25,9 @@ use TYPO3\CMS\Core\Utility\ArrayUtility;
class SettingsFactory
{
/**
* @var array
*/
private $defaults = [
'pageViewsBar' => [
'periodInDays' => 31,

View file

@ -36,7 +36,7 @@ class Pageview
$this->connection = $connection;
}
public function add(Model $pageview)
public function add(Model $pageview): void
{
$this->connection->insert(
'tx_tracking_pageview',

View file

@ -1,6 +1,6 @@
<?php
namespace DanielSiepmann\Tracking\Unit\Domain\Model;
namespace DanielSiepmann\Tracking\Tests\Unit\Domain\Model;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
@ -33,7 +33,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function canBeCreated()
public function canBeCreated(): void
{
$language = $this->prophesize(SiteLanguage::class);
@ -52,7 +52,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function returnsPageUid()
public function returnsPageUid(): void
{
$language = $this->prophesize(SiteLanguage::class);
@ -71,7 +71,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function returnsLanguage()
public function returnsLanguage(): void
{
$language = $this->prophesize(SiteLanguage::class);
@ -90,7 +90,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function returnsCrdate()
public function returnsCrdate(): void
{
$language = $this->prophesize(SiteLanguage::class);
$crdate = new \DateTimeImmutable();
@ -110,7 +110,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function returnsPageType()
public function returnsPageType(): void
{
$language = $this->prophesize(SiteLanguage::class);
@ -129,7 +129,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function returnsUrl()
public function returnsUrl(): void
{
$language = $this->prophesize(SiteLanguage::class);
@ -148,7 +148,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function returnsUserAgent()
public function returnsUserAgent(): void
{
$language = $this->prophesize(SiteLanguage::class);

View file

@ -1,6 +1,6 @@
<?php
namespace DanielSiepmann\Tracking\Unit\Domain\Pageview;
namespace DanielSiepmann\Tracking\Tests\Unit\Domain\Pageview;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
@ -36,7 +36,7 @@ class FactoryTest extends TestCase
/**
* @test
*/
public function returnsPageview()
public function returnsPageview(): void
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
@ -57,7 +57,7 @@ class FactoryTest extends TestCase
/**
* @test
*/
public function returnedPageviewContainsUserAgent()
public function returnedPageviewContainsUserAgent(): void
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
@ -83,7 +83,7 @@ class FactoryTest extends TestCase
/**
* @test
*/
public function returnedPageviewContainsUri()
public function returnedPageviewContainsUri(): void
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
@ -107,7 +107,7 @@ class FactoryTest extends TestCase
/**
* @test
*/
public function returnedPageviewContainsPageType()
public function returnedPageviewContainsPageType(): void
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
@ -131,7 +131,7 @@ class FactoryTest extends TestCase
/**
* @test
*/
public function returnedPageviewContainsDateTime()
public function returnedPageviewContainsDateTime(): void
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
@ -152,7 +152,7 @@ class FactoryTest extends TestCase
/**
* @test
*/
public function returnedPageviewContainsLanguage()
public function returnedPageviewContainsLanguage(): void
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
@ -173,7 +173,7 @@ class FactoryTest extends TestCase
/**
* @test
*/
public function returnedPageviewContainsPageId()
public function returnedPageviewContainsPageId(): void
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);

View file

@ -1,6 +1,6 @@
<?php
namespace DanielSiepmann\Tracking\Unit\Domain\Repository;
namespace DanielSiepmann\Tracking\Tests\Unit\Domain\Repository;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
@ -35,7 +35,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function modelCanBeAdded()
public function modelCanBeAdded(): void
{
$connection = $this->prophesize(Connection::class);

View file

@ -1,6 +1,6 @@
<?php
namespace DanielSiepmann\Tracking\Unit\Middleware;
namespace DanielSiepmann\Tracking\Tests\Unit\Middleware;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
@ -41,7 +41,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function doesNotAddBlacklistedRequest()
public function doesNotAddBlacklistedRequest(): void
{
$repository = $this->prophesize(Repository::class);
$context = $this->prophesize(Context::class);
@ -63,7 +63,7 @@ class PageviewTest extends TestCase
/**
* @test
*/
public function addsPageviewToRepository()
public function addsPageviewToRepository(): void
{
$repository = $this->prophesize(Repository::class);
$context = $this->prophesize(Context::class);

View file

@ -27,6 +27,7 @@
},
"require": {
"doctrine/dbal": "^2.10",
"ext-mbstring": "*",
"php": "^7.3.0",
"psr/http-message": "^1.0",
"psr/http-server-handler": "^1.0",
@ -56,6 +57,9 @@
"require-dev": {
"squizlabs/php_codesniffer": "^3.5",
"phpunit/phpunit": "^9.0",
"typo3/cms-dashboard": "^10.3.0"
"typo3/cms-dashboard": "^10.3.0",
"phpstan/phpstan": "^0.12.18",
"phpstan/extension-installer": "^1.0",
"jangregor/phpstan-prophecy": "^0.6.2"
}
}

10
phpstan.neon Normal file
View file

@ -0,0 +1,10 @@
parameters:
level: max
paths:
- Classes
- Tests
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
ignoreErrors:
- '#Cannot call method fetchAll\(\) on Doctrine\\DBAL\\Driver\\Statement\|int\.#'
- '#Cannot call method fetchColumn\(\) on Doctrine\\DBAL\\Driver\\Statement\|int\.#'