Skip to content

Commit

Permalink
App: upgrade to PHP 8.1, phpstan level 9
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Mar 7, 2023
1 parent 2cd74d3 commit 581cd29
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 29 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,3 @@ jobs:
with:
php: "8.1"
database: tests

test80:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester-mysql.yml@v1
with:
php: "8.0"
database: tests
20 changes: 20 additions & 0 deletions app/Model/Utils/DateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace App\Model\Utils;

use DateTimeInterface;
use Nette\Utils\DateTime as NetteDateTime;

class DateTime extends NetteDateTime
{

public static function fromSafe(string|int|DateTimeInterface|null $time): ?static
{
if ($time === null) {
return null;
}

return self::from($time);
}

}
28 changes: 28 additions & 0 deletions app/Model/Utils/Types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace App\Model\Utils;

use InvalidArgumentException;

class Types
{

public static function forceInt(mixed $input): int
{
if (!is_int($input)) {
throw new InvalidArgumentException(sprintf('Expect int, given %s.', gettype($input)));
}

return $input;
}

public static function forceNumber(mixed $input): float|int
{
if (!is_numeric($input) || is_string($input)) {
throw new InvalidArgumentException(sprintf('Expect number, given %s.', gettype($input)));
}

return $input;
}

}
4 changes: 1 addition & 3 deletions app/Presenters/AbstractPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ abstract class AbstractPresenter extends Presenter

abstract public function createComponentGrid(): DataGrid;

public function changeStatus(mixed $id, string $newStatus): void
public function changeStatus(int $id, string $newStatus): void
{
$id = (int) $id;

if (in_array($newStatus, ['active', 'inactive', 'deleted'], true)) {
$data = ['status' => $newStatus];

Expand Down
4 changes: 2 additions & 2 deletions app/Presenters/BasicPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Presenters;

use App\Model\Utils\DateTime;
use App\UI\TEmptyLayoutView;
use DateTime;
use Dibi\Row;
use Ublaboo\DataGrid\DataGrid;

Expand Down Expand Up @@ -34,7 +34,7 @@ public function createComponentGrid(): DataGrid
->setFormat('j. n. Y');

$grid->addColumnNumber('age', 'Age')
->setRenderer(fn (Row $row): int => $row['birth_date']->diff(new DateTime())->y);
->setRenderer(fn (Row $row): ?int => DateTime::fromSafe($row->asDateTime('birth_date'))?->diff(new DateTime())->y);

return $grid;
}
Expand Down
13 changes: 7 additions & 6 deletions app/Presenters/ColumnsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace App\Presenters;

use App\Model\Utils\DateTime;
use App\Model\Utils\Types;
use App\UI\TEmptyLayoutView;
use DateTime;
use Dibi\Fluent;
use Dibi\Row;
use Ublaboo\DataGrid\AggregationFunction\IAggregationFunction;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function createComponentGrid(): DataGrid
->setSortable();

$grid->addColumnNumber('age', 'Age')
->setRenderer(fn (Row $row): int => $row['birth_date']->diff(new DateTime())->y);
->setRenderer(fn (Row $row): ?int => DateTime::fromSafe($row->asDateTime('birth_date'))?->diff(new DateTime())->y);

$grid->setColumnsHideable();

Expand Down Expand Up @@ -103,15 +104,15 @@ public function processDataSource(mixed $dataSource): void
throw new UnexpectedValueException();
}

$this->idsSum = (int) $dataSource->getConnection()
$this->idsSum = Types::forceInt($dataSource->getConnection()
->select('SUM([id])')
->from($dataSource, '_')
->fetchSingle();
->fetchSingle());

$this->avgAge = round((float) $dataSource->getConnection()
$this->avgAge = round(Types::forceNumber($dataSource->getConnection()
->select('AVG(YEAR([birth_date]))')
->from($dataSource, '_')
->fetchSingle());
->fetchSingle()));
}

public function renderResult(string $key): string
Expand Down
3 changes: 2 additions & 1 deletion app/Presenters/EditPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Presenters;

use App\Model\Utils\DateTime;
use App\UI\TEmptyLayoutView;
use Dibi\Row;
use Nette\Forms\Container;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function createComponentGrid(): DataGrid
$container->setDefaults([
'id' => $row['id'],
'name' => $row['name'],
'birth_date' => $row['birth_date']->format('j. n. Y'),
'birth_date' => DateTime::fromSafe($row->asDateTime('birth_date'))?->format('j. n. Y'),
'link' => $row['name'],
'status' => $row['status'],
]);
Expand Down
4 changes: 2 additions & 2 deletions app/Presenters/FiltersPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Presenters;

use App\Model\Utils\DateTime;
use App\UI\TEmptyLayoutView;
use DateTime;
use Dibi\Fluent;
use Dibi\Row;
use Nette\Utils\ArrayHash;
Expand Down Expand Up @@ -48,7 +48,7 @@ public function createComponentGrid(): DataGrid
->setFilterDateRange();

$grid->addColumnNumber('age', 'Age')
->setRenderer(fn (Row $row): int => $row['birth_date']->diff(new DateTime())->y)
->setRenderer(fn (Row $row): ?int => DateTime::fromSafe($row->asDateTime('birth_date'))?->diff(new DateTime())->y)
->setFilterRange()
->setCondition(function (Fluent $fluent, ArrayHash $values): void {
if ((bool) $values['from']) {
Expand Down
4 changes: 2 additions & 2 deletions app/Presenters/LocalizationPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Presenters;

use App\Model\Utils\DateTime;
use App\UI\TEmptyLayoutView;
use DateTime;
use Dibi\Row;
use Ublaboo\DataGrid\DataGrid;
use Ublaboo\DataGrid\Localization\SimpleTranslator;
Expand Down Expand Up @@ -35,7 +35,7 @@ public function createComponentGrid(): DataGrid
->setFormat('j. n. Y');

$grid->addColumnNumber('age', 'Age')
->setRenderer(fn (Row $row): int => $row['birth_date']->diff(new DateTime())->y);
->setRenderer(fn (Row $row): ?int => DateTime::fromSafe($row->asDateTime('birth_date'))?->diff(new DateTime())->y);

$translator = new SimpleTranslator([
'ublaboo_datagrid.no_item_found_reset' => 'Žádné položky nenalezeny. Filtr můžete vynulovat',
Expand Down
6 changes: 3 additions & 3 deletions app/Presenters/TreeViewPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function createComponentGrid(): DataGrid
/**
* @return Fluent<mixed>
*/
public function getChildren(mixed $parentCategoryId): Fluent
public function getChildren(int $parentCategoryId): Fluent
{
$join = $this->dibiConnection->select('COUNT(id) AS count, parent_category_id')
->from('categories')
Expand All @@ -82,10 +82,10 @@ public function getChildren(mixed $parentCategoryId): Fluent
->from('categories', 'c')
->leftJoin($join, 'c_b')
->on('c_b.parent_category_id = c.id')
->where('c.parent_category_id = ?', (int) $parentCategoryId);
->where('c.parent_category_id = ?', $parentCategoryId);
}

public function handleSort(mixed $itemId, mixed $prevId, mixed $nextId, mixed $parentId): void
public function handleSort(int $itemId, int $prevId, int $nextId, int $parentId): void
{
$this->flashMessage(
sprintf(
Expand Down
2 changes: 1 addition & 1 deletion app/UI/TEmptyLayoutView.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function renderDefault(): void
{
$request = $this->getRequest();

if ($request->getParameter('inFrame') === true) {
if ($request !== null && $request->getParameter('inFrame') === true) {
$this->setLayout(__DIR__ . '/../templates/@layout.inFrame.latte');
}
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "MIT",
"type": "project",
"require": {
"php": ">= 8.0",
"php": ">= 8.1",

"nette/application": "^3.1.0",
"nette/bootstrap": "^3.2.0",
Expand Down
10 changes: 9 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ includes:
- vendor/phpstan/phpstan-dibi/extension.neon

parameters:
level: 8
level: 9
phpVersion: 80100

tmpDir: %currentWorkingDirectory%/temp/phpstan

paths:
- app

fileExtensions:
- php

ignoreErrors:

0 comments on commit 581cd29

Please sign in to comment.