Skip to content

Commit

Permalink
add phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 23, 2024
1 parent f6d2b2d commit ace47b2
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 43 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: "Build"

on:
pull_request:
push:
branches:
- main

env:
php-tools: "composer:v2"

jobs:
phpstan:
name: PHPStan

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP with pecl extension
uses: shivammathur/setup-php@v2
with:
php-version: 8.3

- name: Get composer cache directory
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --prefer-dist

- name: Run PHPStan
run: composer phpstan
5 changes: 5 additions & 0 deletions .phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
treatPhpDocTypesAsCertain: false
paths:
- app
17 changes: 11 additions & 6 deletions app/Blog/Presenters/HomePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Nette\Application\UI\Form;
use Nette\Application\UI\Presenter;
use Nette\DI\Attributes\Inject;
use Nette\Utils\ArrayHash;
use OrmDemo\Blog\Model\Comments\Comment;
use OrmDemo\Blog\Model\Posts\Post;
use OrmDemo\Model\Orm;
Expand All @@ -15,7 +16,7 @@ class HomePresenter extends Presenter
#[Inject]
public Orm $orm;

private Post $post;
private Post|null $post = null;


public function renderDefault(): void
Expand All @@ -34,6 +35,8 @@ public function actionDetail(int $id): void

public function renderDetail(int $id): void
{
if (!$this->post) $this->error();

$template = $this->createTemplate(PostDetailTemplate::class);
$template->post = $this->post;
$this->sendTemplate($template);
Expand All @@ -52,8 +55,10 @@ protected function createComponentAddCommentForm(): Form
}


public function processAddCommentForm(Form $form, $values): void
public function processAddCommentForm(Form $form, ArrayHash $values): void
{
if (!$this->post) $this->error();

$comment = new Comment();
$comment->content = $values->content;
$comment->name = $values->name;
Expand All @@ -76,9 +81,7 @@ public function handleDeleteComment(int $commentId): void

public function createComponentUpdateTagsForm(): Form
{
if (!$this->post) {
$this->error();
}
if (!$this->post) $this->error();

$form = new Form;
$form->addCheckboxList('tags', 'Tags', $this->orm->tags->findAll()->fetchPairs('id', 'name'))
Expand All @@ -90,8 +93,10 @@ public function createComponentUpdateTagsForm(): Form
}


public function processUpdateTagsForm(Form $form, $values): void
public function processUpdateTagsForm(Form $form, ArrayHash $values): void
{
if (!$this->post) $this->error();

$this->post->tags->set($values->tags);
$this->orm->posts->persistAndFlush($this->post);
$this->redirect('this');
Expand Down
5 changes: 3 additions & 2 deletions app/Blog/Presenters/LayoutTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

abstract class LayoutTemplate extends Template
{
public string $basePath;
public array $flashes;
public string $basePath;
/** @var list<\stdClass> */
public array $flashes;
}
26 changes: 14 additions & 12 deletions app/Presenters/Error4xxPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@
*/
final class Error4xxPresenter extends Nette\Application\UI\Presenter
{
// allow access via all HTTP methods
public array $allowedMethods = [];
// allow access via all HTTP methods
/** @var list<string> */
public array $allowedMethods = [];


public function renderDefault(Nette\Application\BadRequestException $exception): void
{
// renders the appropriate error template based on the HTTP status code
$code = $exception->getCode();
$file = is_file($file = __DIR__ . "/templates/Error/$code.latte")
? $file
: __DIR__ . '/templates/Error/4xx.latte';
$this->template->httpCode = $code;
$this->template->setFile($file);
}
public function renderDefault(Nette\Application\BadRequestException $exception): void
{
// renders the appropriate error template based on the HTTP status code
$code = $exception->getCode();
$file = is_file($file = __DIR__ . "/templates/Error/$code.latte")
? $file
: __DIR__ . '/templates/Error/4xx.latte';
assert($this->template instanceof Nette\Bridges\ApplicationLatte\Template);
$this->template->httpCode = $code;
$this->template->setFile($file);
}
}
43 changes: 20 additions & 23 deletions app/Presenters/Error5xxPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@
*/
final class Error5xxPresenter implements Nette\Application\IPresenter
{
// allow access via all HTTP methods
public array $allowedMethods = [];


public function __construct(
private ILogger $logger,
) {
}


public function run(Nette\Application\Request $request): Nette\Application\Response
{
// Log the exception
$exception = $request->getParameter('exception');
$this->logger->log($exception, ILogger::EXCEPTION);

// Display a generic error message to the user
return new Responses\CallbackResponse(function (Http\IRequest $httpRequest, Http\IResponse $httpResponse): void {
if (preg_match('#^text/html(?:;|$)#', (string) $httpResponse->getHeader('Content-Type'))) {
require __DIR__ . '/templates/Error/500.phtml';
}
});
}
public function __construct(
private ILogger $logger,
)
{
}


public function run(Nette\Application\Request $request): Nette\Application\Response
{
// Log the exception
$exception = $request->getParameter('exception');
$this->logger->log($exception, ILogger::EXCEPTION);

// Display a generic error message to the user
return new Responses\CallbackResponse(function (Http\IRequest $httpRequest, Http\IResponse $httpResponse): void {
if (preg_match('#^text/html(?:;|$)#', (string)$httpResponse->getHeader('Content-Type'))) {
require __DIR__ . '/templates/Error/500.phtml';
}
});
}
}
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,23 @@
"nextras/migrations": "^3.3",
"contributte/console": "^0.10"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
"nextras/orm-phpstan": "@dev",
"phpstan/phpstan-nette": "^1.2",
"phpstan/extension-installer": "^1.3"
},
"autoload": {
"psr-4": {
"OrmDemo\\": "app"
}
},
"scripts": {
"phpstan": "phpstan analyze -c .phpstan.neon"
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}

0 comments on commit ace47b2

Please sign in to comment.