Skip to content

Instantly share code, notes, and snippets.

@84m
Created January 16, 2019 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 84m/1cb41b41fcacf65d1017f4f1c8c09700 to your computer and use it in GitHub Desktop.
Save 84m/1cb41b41fcacf65d1017f4f1c8c09700 to your computer and use it in GitHub Desktop.
phpstan example
{
"name": "foo",
"type": "project",
"require": {
"symfony/http-foundation": "^4.2",
"phpstan/phpstan": "^0.11.0"
},
"autoload": {
"classmap": [
"Controller.php"
]
}
}
<?php declare(strict_types = 1);
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class Controller
{
public function getFoo(Request $request)
{
$userId = $request->get('userid');
if (isset($userId)) {
$userId = (int)($userId);
}
// ...
$response = new Response();
if (isset($userId)) {
$response->headers->add(['userId' => $userId]);
}
return $response;
}
}
$ vender/bin/phpstan analyse --level max Controller.php
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
------ ----------------------------------------------------------------
Line Controller.php
------ ----------------------------------------------------------------
21 Variable $userId in isset() always exists and is not nullable.
------ ----------------------------------------------------------------
[ERROR] Found 1 error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment