Skip to content

Commit

Permalink
Code: apply phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jan 2, 2024
1 parent 19761c4 commit b606f94
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 126 deletions.
97 changes: 23 additions & 74 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
includes:
#- vendor/contributte/phpstan/phpstan.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-nette/extension.neon
- vendor/phpstan/phpstan-nette/rules.neon
#- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/contributte/phpstan/phpstan.neon
- extension.neon

parameters:
level: 8
Expand All @@ -19,73 +16,25 @@ parameters:
- src
- .docs

ignoreErrors:
-
message: """
#^Call to deprecated method getName\\(\\) of class Contributte\\\\Imagist\\\\Bridge\\\\Doctrine\\\\ImageType\\:
this method will be removed in Doctrine DBAL 4\\.0,
use \\{@see TypeRegistry\\:\\:lookupName\\(\\)\\} instead\\.$#
"""
count: 2
path: src/Bridge/Doctrine/ImageType.php

-
message: "#^Class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\GumletImageStorageExtension extends @final class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\GumletImagistExtension\\.$#"
count: 1
path: src/Bridge/Nette/DI/GumletImageStorageExtension.php

-
message: "#^Class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\ImageStorageConfigFiltersExtension extends @final class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\ImagistNeonFiltersExtension\\.$#"
count: 1
path: src/Bridge/Nette/DI/ImageStorageConfigFiltersExtension.php

-
message: "#^Class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\ImageStorageExtension extends @final class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\ImagistExtension\\.$#"
count: 1
path: src/Bridge/Nette/DI/ImageStorageExtension.php

-
message: "#^Method Contributte\\\\Imagist\\\\Bridge\\\\Symfony\\\\Serializer\\\\ImagistNormalizer\\:\\:normalize\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php

-
message: "#^Method Contributte\\\\Imagist\\\\Bridge\\\\Symfony\\\\Serializer\\\\ImagistNormalizer\\:\\:normalize\\(\\) return type with generic class ArrayObject does not specify its types\\: TKey, TValue$#"
count: 1
path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php

-
message: "#^Method Contributte\\\\Imagist\\\\Bridge\\\\Symfony\\\\Serializer\\\\ImagistNormalizer\\:\\:normalize\\(\\) should return array\\|ArrayObject\\|bool\\|float\\|int\\|string\\|null but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#"
count: 1
path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php

-
message: "#^Method Contributte\\\\Imagist\\\\Bridge\\\\Symfony\\\\Serializer\\\\ImagistNormalizer\\:\\:supportsNormalization\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php

-
message: "#^PHPDoc tag @return with type mixed is not subtype of native type array\\|ArrayObject\\|bool\\|float\\|int\\|string\\|null\\.$#"
count: 1
path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php

-
message: "#^Method Contributte\\\\Imagist\\\\Entity\\\\PromisedImage\\:\\:getOriginal\\(\\) should return Contributte\\\\Imagist\\\\Entity\\\\PromisedImage but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#"
count: 1
path: src/Entity/PromisedImage.php

-
message: "#^Method Contributte\\\\Imagist\\\\Entity\\\\PromisedImage\\:\\:withFilter\\(\\) should return Contributte\\\\Imagist\\\\Entity\\\\PromisedImage but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#"
count: 1
path: src/Entity/PromisedImage.php

-
message: "#^Method Contributte\\\\Imagist\\\\Entity\\\\PromisedImage\\:\\:withName\\(\\) should return Contributte\\\\Imagist\\\\Entity\\\\PromisedImage but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#"
count: 1
path: src/Entity/PromisedImage.php

-
message: "#^Method Contributte\\\\Imagist\\\\Entity\\\\PromisedImage\\:\\:withScope\\(\\) should return Contributte\\\\Imagist\\\\Entity\\\\PromisedImage but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#"
count: 1
path: src/Entity/PromisedImage.php
excludePaths:
- %currentWorkingDirectory%/tests/_*

ignoreErrors:
- '#^Only booleans are allowed in a negated boolean, string\|null given.#'
- '#(.+)should be contravariant(.+)#'

- '#^Property Contributte\\Codeception\\Module\\NetteApplicationModule::\$path is never read, only written.#'

# Should not happen
- '#^Method Contributte\\Codeception\\Module\\NetteDIModule::getContainer\(\) should return Nette\\DI\\Container but returns Nette\\DI\\Container\|null#'
- '#^Method Contributte\\Codeception\\Http\\Request::getFile\(\) should return Nette\\Http\\FileUpload\|null but returns array\|Nette\\Http\\FileUpload\|null.#'
- '#Parameter \#1 \$path of static method Nette\\Utils\\FileSystem::delete\(\) expects string, string\|false given\.#'
- '#Call to an undefined method Nette\\DI\\Definitions\\Definition::setFactory\(\)\.#'
- '#Call to protected method setType\(\) of class Nette\\DI\\Definitions\\Definition\.#'
- '#.+Contributte\\Codeception\\Module\\NetteApplicationModule::\$config.+#'
- '#.+Contributte\\Codeception\\Module\\NetteDIModule::\$config.+#'
- '#.+Contributte\\Codeception\\Module\\NetteDIModule::\$requiredFields.+#'

earlyTerminatingMethodCalls:
Codeception\Module:
- fail
3 changes: 2 additions & 1 deletion src/Connector/NetteConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function setContainerAccessor(callable $containerAccessor): void
* @param Request $request
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function doRequest($request): Response
public function doRequest(object $request): Response
{
$phpSelf = $_SERVER['PHP_SELF'];
$_COOKIE = $request->getCookies();
Expand Down Expand Up @@ -67,6 +67,7 @@ public function doRequest($request): Response
$content = (string) ob_get_clean();
} catch (Throwable $e) {
ob_end_clean();

throw $e;
}

Expand Down
21 changes: 5 additions & 16 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
class Request implements IRequest
{

/** @var RequestFactory */
private $factory;
private RequestFactory $factory;

/** @var IRequest */
private $request;
private IRequest $request;

public function __construct(RequestFactory $factory)
{
Expand All @@ -30,10 +28,7 @@ public function reset(): void
$this->request = $this->factory->fromGlobals();
}

/**
* @return mixed
*/
public function getCookie(string $key)
public function getCookie(string $key): mixed
{
return $this->request->getCookie($key);
}
Expand Down Expand Up @@ -77,18 +72,12 @@ public function getMethod(): string
return $this->request->getMethod();
}

/**
* @return mixed
*/
public function getPost(?string $key = null)
public function getPost(?string $key = null): mixed
{
return func_num_args() === 0 ? $this->request->getPost() : $this->request->getPost($key);
}

/**
* @return mixed
*/
public function getQuery(?string $key = null)
public function getQuery(?string $key = null): mixed
{
return func_num_args() === 0 ? $this->request->getQuery() : $this->request->getQuery($key);
}
Expand Down
28 changes: 16 additions & 12 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ class Response implements IResponse
{

/** @var string The domain in which the cookie will be available */
public $cookieDomain = '';
public string $cookieDomain = '';

/** @var string The path in which the cookie will be available */
public $cookiePath = '/';
public string $cookiePath = '/';

/** @var bool Whether the cookie is available only through HTTPS */
public $cookieSecure = false;
public bool $cookieSecure = false;

/** @var int */
private $code = self::S200_OK;
private int $code = self::S200_OK;

/** @var string[] */
private $headers = [];
private array $headers = [];

public function reset(): void
{
Expand All @@ -42,36 +41,40 @@ public function getCode(): int
/**
* @return static
*/
public function setCode(int $code, ?string $reason = null)
public function setCode(int $code, ?string $reason = null): static
{
$this->code = $code;

return $this;
}

/**
* @return static
*/
public function setHeader(string $name, string $value)
public function setHeader(string $name, string $value): static
{
$this->headers[$name] = $value;

return $this;
}

/**
* @return static
*/
public function addHeader(string $name, string $value)
public function addHeader(string $name, string $value): static
{
$this->headers[$name] = $value;

return $this;
}

/**
* @return static
*/
public function setContentType(string $type, ?string $charset = null)
public function setContentType(string $type, ?string $charset = null): static
{
$this->setHeader('Content-Type', $type . ($charset !== null ? '; charset=' . $charset : ''));

return $this;
}

Expand All @@ -84,7 +87,7 @@ public function redirect(string $url, int $code = self::S302_Found): void
/**
* @return static
*/
public function setExpiration(?string $time)
public function setExpiration(?string $time): static
{
if (!$time) {
$this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
Expand Down Expand Up @@ -122,13 +125,14 @@ public function getHeaders(): array
* @param string|int|DateTimeInterface $time
* @return static
*/
public function setCookie(string $name, string $value, $time, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httpOnly = null, ?string $sameSite = null)
public function setCookie(string $name, string $value, mixed $time, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httpOnly = null, ?string $sameSite = null): static
{
return $this;
}

public function deleteCookie(string $name, ?string $path = null, ?string $domain = null, ?bool $secure = null): void
{
// No-op
}

}
6 changes: 3 additions & 3 deletions src/Module/NetteApplicationModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
class NetteApplicationModule extends Framework
{

/** @var array<string, mixed> */
/** @var array<string, mixed> */
protected array $config = [
'followRedirects' => true,
];

private string $path;
private ?string $path = null;

/**
* @param array{path?: string} $settings
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function _beforeSuite($settings = []): void
public function _beforeSuite(mixed $settings = []): void
{
assert(isset($settings['path']));

Expand Down
19 changes: 6 additions & 13 deletions src/Module/NetteDIModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class NetteDIModule extends Module
{

/** @var callable[] function(Container $configurator): void; */
public $onCreateConfigurator = [];
public array $onCreateConfigurator = [];

/** @var callable[] function(Container $container): void; */
public $onCreateContainer = [];
public array $onCreateContainer = [];

/** @var array<string, mixed> */
protected array $config = [
Expand All @@ -38,14 +38,12 @@ class NetteDIModule extends Module
'tempDir',
];

/** @var string */
private $path;
private string $path;

/** @var string[] */
private $configFiles = [];
private array $configFiles = [];

/** @var Container|null */
private $container;
private ?Container $container = null;

/**
* @param array{path?: string} $settings
Expand Down Expand Up @@ -106,10 +104,9 @@ public function getContainer(): Container
}

/**
* @return object
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function grabService(string $service)
public function grabService(string $service): object
{
try {
/** @phpstan-var class-string $service */
Expand Down Expand Up @@ -164,13 +161,11 @@ private function createContainer(): void
}
}


private function getTempDir(): string
{
return $this->path . '/' . $this->config['tempDir'];
}


private function clearTempDir(): void
{
$this->deleteTempDir();
Expand All @@ -179,7 +174,6 @@ private function clearTempDir(): void
FileSystem::createDir($tempDir);
}


private function deleteTempDir(): void
{
$tempDir = $this->getTempDir();
Expand All @@ -188,7 +182,6 @@ private function deleteTempDir(): void
}
}


private function stopContainer(): void
{
if ($this->container === null) {
Expand Down
10 changes: 5 additions & 5 deletions src/Qa/PhpStan/NetteDIModuleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
if ($methodCall->args === []) {
return $methodReflection->getReturnType();
return $methodReflection->getReturnType(); // @phpstan-ignore-line
}

$arg = $methodCall->getArgs()[0]->value;

if (!$arg instanceof ClassConstFetch) {
return $methodReflection->getReturnType();
return $methodReflection->getReturnType(); // @phpstan-ignore-line
}

$class = $arg->class;

if (!$class instanceof Name) {
return $methodReflection->getReturnType();
return $methodReflection->getReturnType(); // @phpstan-ignore-line
}

$class = (string) $class;

if ($class === 'static') {
return $methodReflection->getReturnType();
return $methodReflection->getReturnType(); // @phpstan-ignore-line
}

if ($class === 'self') {
$class = $scope->getClassReflection()->getName();
$class = $scope->getClassReflection()->getName(); // @phpstan-ignore-line
}

return new ObjectType($class);
Expand Down
Loading

0 comments on commit b606f94

Please sign in to comment.