Skip to content

Commit

Permalink
Merge pull request #160 from koriym/sa
Browse files Browse the repository at this point in the history
Bump phpstan 1.0
  • Loading branch information
koriym committed Jan 8, 2022
2 parents f4fdd69 + ebca348 commit 6ecbf33
Show file tree
Hide file tree
Showing 14 changed files with 569 additions and 374 deletions.
7 changes: 5 additions & 2 deletions composer.json
Expand Up @@ -34,7 +34,10 @@
"bamarni/composer-bin-plugin": "^1.4"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"bamarni/composer-bin-plugin": true
}
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -65,7 +68,7 @@
"cs": ["phpcs --standard=./phpcs.xml src tests"],
"cs-fix": ["./vendor/bin/phpcbf src tests"],
"clean": ["./vendor/bin/phpstan clear-result-cache", "./vendor/bin/psalm --clear-cache", "rm -rf tests/tmp/*.php"],
"sa": ["./vendor/bin/phpstan analyse -c phpstan.neon", "psalm --show-info=true"],
"sa": ["psalm --monochrome --show-info=true", "./vendor/bin/phpstan analyse --no-ansi --no-progress -c phpstan.neon"],
"metrics": ["./vendor/bin/phpmetrics --report-html=build/metrics --exclude=Exception --junit=build/junit.xml src"],
"phpmd": ["./vendor/bin/phpmd --exclude src/Annotation src text ./phpmd.xml"],
"build": ["@cs", "@sa", "@pcov", "@metrics"]
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml
Expand Up @@ -38,6 +38,7 @@
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification"/>
<!-- /Base -->
<!-- Option -->
<exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed"/>
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Expand Up @@ -3,6 +3,6 @@ parameters:
paths:
- src
- tests
excludes_analyse:
excludePaths:
- */tests/*/tmp/*
- */tests/Fake/*
10 changes: 4 additions & 6 deletions src/Extension/Router/RouterInterface.php
Expand Up @@ -8,8 +8,8 @@

/**
* @psalm-type Globals = array{
* _GET: array<string, string|array>,
* _POST: array<string, string|array>
* _GET: array<string, mixed>,
* _POST: array<string, mixed>
* }
* @psalm-type Server = array{
* REQUEST_URI: string,
Expand All @@ -22,10 +22,8 @@
interface RouterInterface extends ExtensionInterface
{
/**
* @psalm-param Globals $globals
* @psalm-param Server $server
* @phpstan-param array<string, mixed> $globals
* @phpstan-param array<string, mixed> $server
* @param Globals $globals
* @param Server $server
*
* @return RouterMatch
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Extension/Router/RouterMatch.php
Expand Up @@ -29,6 +29,16 @@ class RouterMatch
*/
public $query = [];

/**
* @param array<string, mixed> $query
*/
public function __construct(string $method = '', string $path = '', array $query = [])
{
$this->method = $method;
$this->path = $path;
$this->query = $query;
}

public function __toString(): string
{
$querySymbol = $this->query ? '?' : '';
Expand Down
32 changes: 20 additions & 12 deletions src/Provide/Router/WebRouter.php
Expand Up @@ -28,7 +28,10 @@
*/
final class WebRouter implements RouterInterface
{
/** @var string */
/**
* @readonly
* @var string
*/
private $schemeHost;

/**
Expand All @@ -42,16 +45,19 @@ public function __construct(string $schemeHost)

/**
* {@inheritdoc}
*
* @param Globals $globals
* @param Server $server
*/
public function match(array $globals, array $server)
{
$method = strtolower($server['REQUEST_METHOD']);
$match = new RouterMatch();
$match->method = $method;
$match->path = $this->schemeHost . parse_url($server['REQUEST_URI'], PHP_URL_PATH);
$match->query = $method === 'get' ? $globals['_GET'] : $this->getUnsafeQuery($method, $globals, $server);

return $match;
return new RouterMatch(
$method,
$this->schemeHost . parse_url($server['REQUEST_URI'], PHP_URL_PATH),
$this->getQuery($method, $globals, $server)
);
}

/**
Expand All @@ -65,15 +71,17 @@ public function generate($name, $data)
/**
* Return request query by media-type
*
* @psalm-param Server $server
* @psalm-param Globals $globals
* @phpstan-param array<string, mixed> $globals
* @phpstan-param array<string, mixed> $server
* @param Server $server
* @param Globals $globals
*
* @return array<string, mixed> $globals
* @return array<string, mixed>
*/
private function getUnsafeQuery(string $method, array $globals, array $server): array
private function getQuery(string $method, array $globals, array $server): array
{
if ($method === 'get') {
return $globals['_GET'];
}

if ($method === 'post') {
return $globals['_POST'];
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Fake/Inject/PsrLoggerApplication.php
Expand Up @@ -2,11 +2,13 @@

namespace BEAR\Sunday\Inject;

use Psr\Log\LoggerInterface;

class PsrLoggerApplication
{
use PsrLoggerInject;

public function returnDependency(): \Psr\Log\LoggerInterface
public function returnDependency(): LoggerInterface
{
return $this->logger;
}
Expand Down
5 changes: 2 additions & 3 deletions tests/Fake/Inject/PsrLoggerModule.php
Expand Up @@ -3,13 +3,12 @@
namespace BEAR\Sunday\Inject;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Ray\Di\AbstractModule;

class PsrLoggerModule extends AbstractModule
{
protected function configure(): void
protected function configure()
{
$this->bind(LoggerInterface::class)->to(NullLogger::class);
$this->bind(LoggerInterface::class)->toNull();
}
}
7 changes: 5 additions & 2 deletions tests/Inject/PsrLoggerInjectTest.php
Expand Up @@ -5,14 +5,17 @@
namespace BEAR\Sunday\Inject;

use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Psr\Log\LoggerInterface;
use Ray\Di\Injector;

use function assert;

class PsrLoggerInjectTest extends TestCase
{
public function testInjectTrait(): void
{
$app = (new Injector(new PsrLoggerModule()))->getInstance(__NAMESPACE__ . '\PsrLoggerApplication');
$this->assertInstanceOf(NullLogger::class, $app->returnDependency());
assert($app instanceof PsrLoggerApplication);
$this->assertInstanceOf(LoggerInterface::class, $app->returnDependency());
}
}
6 changes: 5 additions & 1 deletion tests/Module/Constant/NamedModuleTest.php
Expand Up @@ -8,6 +8,8 @@
use PHPUnit\Framework\TestCase;
use Ray\Di\Injector;

use function assert;

class NamedModuleTest extends TestCase
{
/** @var FakeApplication */
Expand All @@ -19,7 +21,9 @@ protected function setUp(): void
'path' => __DIR__,
'id' => 'bear',
];
$this->app = (new Injector(new NamedModule($names)))->getInstance(FakeApplication::class);
$app = (new Injector(new NamedModule($names)))->getInstance(FakeApplication::class);
assert($app instanceof FakeApplication);
$this->app = $app;
}

public function testNamed(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Provide/Error/ThrowableHandlerTest.php
Expand Up @@ -43,7 +43,7 @@ public function testError(): void
{
try {
echo hello; // @phpstan-ignore-line
} catch (Throwable $e) {
} catch (Throwable $e) { // @phpstan-ignore-line create $e
}

$this->throableHandler->handle($e, new RouterMatch())->transfer(); // @phpstan-ignore-line
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Expand Up @@ -6,7 +6,7 @@
use Ray\ServiceLocator\ServiceLocator;

require dirname(__DIR__) . '/vendor/autoload.php';
array_map('unlink', (array) glob(__DIR__ . '/tmp/*.php'));
array_map('unlink', (array) glob(__DIR__ . '/tmp/*.php')); // @phpstan-ignore-line

// no annotation in PHP 8
if (PHP_MAJOR_VERSION >= 8) {
Expand Down
8 changes: 7 additions & 1 deletion vendor-bin/tools/composer.json
Expand Up @@ -3,9 +3,15 @@
"doctrine/coding-standard": "^8.2",
"phpmd/phpmd": "^2.9",
"phpmetrics/phpmetrics": "^2.7",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan": "^1.3",
"psalm/plugin-phpunit": "^0.13",
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^4.2"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/package-versions-deprecated": true
}
}
}

0 comments on commit 6ecbf33

Please sign in to comment.