Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
style(phpstan): Fix issues reported by new phpstan rules
Browse files Browse the repository at this point in the history
For now set: `checkMissingIterableValueType: false`
  • Loading branch information
k911 committed Jan 30, 2020
1 parent 8e03a43 commit b683a8d
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 25 deletions.
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
parameters:
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
excludes_analyse:
- src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
ignoreErrors:
# Put false positives here

# Swoole/PHPStan internal errors
- '#Internal error\: Internal error\: Expected to find an ancestor with class name Swoole\\Timer on Swoole\\Server, but none was found\..*#'

9 changes: 6 additions & 3 deletions phpstan.tests.neon
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
parameters:
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
excludes_analyse:
- tests/Fixtures/Symfony/app/var
- tests/Fixtures/Symfony/app/TestAppKernel
ignoreErrors:
# Put false positives here

# On purpose
- '#K911\\Swoole\\Tests\\Unit\\Server\\Swoole(:?Http)?Server(:?Mock|Dummy)::__construct\(\) does not call parent constructor from Swoole\\#'

# Symfony configuration files
- '#Undefined variable: \$container#'

# Swoole/PHPStan internal errors
- '#Internal error\: Internal error\: Expected to find an ancestor with class name Swoole\\Timer on Swoole\\Server, but none was found\..*#'
- '#Internal error\: Internal error\: Expected to find an ancestor with class name Swoole\\Timer on Swoole\\Http\\Server, but none was found\..*#'

Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@

abstract class AbstractServerStartCommand extends Command
{
/**
* @var ParameterBagInterface
*/
protected $parameterBag;

private $server;
private $bootManager;
private $serverConfiguration;
private $serverConfigurator;

/**
* @var bool
*/
private $testing = false;

public function __construct(
Expand Down
1 change: 0 additions & 1 deletion src/Bridge/Symfony/HttpFoundation/ResponseProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function process(HttpFoundationResponse $httpFoundationResponse, SwooleRe
}

foreach ($httpFoundationResponse->headers->allPreserveCaseWithoutCookies() as $name => $values) {
/** @var array $values */
foreach ($values as $value) {
$swooleResponse->header($name, (string) $value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function getId(): string
*
* @throws \Exception
*/
public function setId($id): void
public function setId(string $id): void
{
if ($this->started) {
throw new LogicException('Cannot set session ID after the session has started.');
Expand All @@ -179,15 +179,15 @@ public function setId($id): void
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* {@inheritdoc}
*/
public function setName($name): void
public function setName(string $name): void
{
$this->name = $name;
}
Expand All @@ -197,7 +197,7 @@ public function setName($name): void
*
* @throws \Assert\AssertionFailedException
*/
public function getBag($name): SessionBagInterface
public function getBag(string $name): SessionBagInterface
{
if (!isset($this->bags[$name])) {
throw new \InvalidArgumentException(\sprintf('The SessionBagInterface `%s` is not registered.', $name));
Expand Down
10 changes: 10 additions & 0 deletions src/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public function connect(int $timeout = 3, float $step = 0.1): bool
return false;
}

/**
* @param null|mixed $data
*
* @return array<string, array<string, mixed>>
*/
public function send(string $path, string $method = Http::METHOD_GET, array $headers = [], $data = null, int $timeout = 3): array
{
$this->assertHttpMethodSupported($method);
Expand Down Expand Up @@ -126,6 +131,8 @@ public function serialize(): string

/**
* {@inheritdoc}
*
* @param string $serialized
*/
public function unserialize($serialized): void
{
Expand Down Expand Up @@ -155,6 +162,9 @@ private function assertHttpMethodSupported(string $method): void
throw UnsupportedHttpMethodException::forMethod($method, self::SUPPORTED_HTTP_METHODS);
}

/**
* @param mixed $data
*/
private function serializeRequestData(Client $client, $data): void
{
$json = \json_encode($data, \JSON_THROW_ON_ERROR);
Expand Down
10 changes: 9 additions & 1 deletion src/Common/XdebugHandler/XdebugHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ private function generateLoadedPhpIniFiles(): Generator
yield $loadedIniFile;
}

foreach (\explode(',', \php_ini_scanned_files()) as $scanned) {
$files = \php_ini_scanned_files();
if (false === $files) {
$files = '';
}

foreach (\explode(',', $files) as $scanned) {
$preparedScanned = \trim($scanned);

if ('' !== $preparedScanned) {
Expand All @@ -140,6 +145,9 @@ private function parsePhpIniContent(iterable $iniFiles): string
// Merge loaded settings into our ini content, if it is valid
if ($config = \parse_ini_string($content)) {
$loaded = \ini_get_all(null, false);
if (false === $loaded) {
$loaded = [];
}
$content .= $this->mergeLoadedConfig($loaded, $config);
}

Expand Down
30 changes: 28 additions & 2 deletions src/Component/GeneratedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,61 @@
use Generator;
use IteratorAggregate;

/**
* @template T
* @implements IteratorAggregate<T>
*/
final class GeneratedCollection implements IteratorAggregate
{
private $itemCollection;
private $items;

/**
* @param iterable<mixed> $itemCollection
* @param mixed ...$items
* @param iterable<T> $itemCollection
* @param T ...$items
*/
public function __construct(iterable $itemCollection, ...$items)
{
$this->itemCollection = $itemCollection;
$this->items = $items;
}

/**
* @throws \Exception
*
* @return Generator<T>
*/
public function each(callable $func): Generator
{
foreach ($this->getIterator() as $item) {
yield $func($item);
}
}

/**
* @throws \Exception
*
* @return GeneratedCollection<T>
*/
public function map(callable $func): self
{
return new self($this->each($func));
}

/**
* @throws \Exception
*
* @return GeneratedCollection<T>
*/
public function filter(callable $func): self
{
return new self($this->filterItems($func));
}

/**
* {@inheritdoc}
*
* @return Generator<T>
*/
public function getIterator(): Generator
{
Expand All @@ -49,6 +70,11 @@ public function getIterator(): Generator
yield from $this->items;
}

/**
* @throws \Exception
*
* @return Generator<T>
*/
private function filterItems(callable $func): Generator
{
foreach ($this->getIterator() as $item) {
Expand Down
9 changes: 6 additions & 3 deletions src/Coroutine/CoroutinePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ final class CoroutinePool
{
private $coroutines;
private $coroutinesCount;
private $results = [];
private $exceptions = [];
private $resultsChannel;
private $started = false;
private $results;
private $exceptions;
private $started;

public function __construct(Channel $resultsChannel, callable ...$coroutines)
{
$this->coroutines = $coroutines;
$this->coroutinesCount = \count($coroutines);
$this->resultsChannel = $resultsChannel;
$this->results = [];
$this->exceptions = [];
$this->started = false;
}

public static function fromCoroutines(callable ...$coroutines): self
Expand Down
3 changes: 3 additions & 0 deletions src/Server/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public function getServer(): Server
return $this->server;
}

/**
* @param mixed $data
*/
public function dispatchTask($data): void
{
$this->getServer()->task($data);
Expand Down
10 changes: 10 additions & 0 deletions src/Server/HttpServerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ class HttpServerConfiguration
];

private $sockets;

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

/**
* @var array<string, mixed>
*/
private $settings;

/**
Expand Down Expand Up @@ -296,6 +304,8 @@ private function initializeSettings(array $init): void
}

/**
* @param array<string, mixed> $settings
*
* @throws \Assert\AssertionFailedException
*/
private function setSettings(array $settings): void
Expand Down
9 changes: 7 additions & 2 deletions src/Server/RequestHandler/LimitedRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ final class LimitedRequestHandler implements RequestHandlerInterface, BootableIn
private $server;
private $requestCounter;
private $decorated;

/**
* @var null|SymfonyStyle
*/
private $symfonyStyle;

public function __construct(RequestHandlerInterface $decorated, HttpServer $server, AtomicCounter $counter)
{
$this->decorated = $decorated;
$this->server = $server;
$this->requestCounter = $counter;
$this->requestLimit = -1;
}

/**
Expand Down Expand Up @@ -68,12 +73,12 @@ public function handle(Request $request, Response $response): void
}
}

private function console(callable $callback)
private function console(callable $callback): void
{
if (!$this->symfonyStyle instanceof SymfonyStyle) {
throw new InvalidArgumentException('To interact with console, SymfonyStyle object must be provided as "symfonyStyle" attribute.');
}

return $callback($this->symfonyStyle);
$callback($this->symfonyStyle);
}
}
11 changes: 4 additions & 7 deletions src/Server/Session/SwooleTableStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public function __construct(Table $sharedMemory, int $maxSessionDataBytes = 1024
$this->maxSessionDataBytes = $maxSessionDataBytes;
}

/**
* @return static
*/
public static function fromDefaults(int $maxActiveSessions = 1024, int $maxSessionDataBytes = 1024, float $tableConflictProportion = 0.2): self
{
return new self(
Expand Down Expand Up @@ -108,12 +105,12 @@ public function get(string $key, ?callable $expired = null): ?string
/** @var Table\Row $row */
$row = $this->sharedMemory->get($key);

/**
* @var int
* @var string $data
*/
/** @var int $expiresAt */
$expiresAt = $row[self::TABLE_COLUMN_EXPIRES_AT];

/** @var string $data */
$data = $row[self::TABLE_COLUMN_DATA];

if (\time() >= $expiresAt) {
if (null !== $expired) {
$expired($key, $data);
Expand Down
3 changes: 3 additions & 0 deletions src/Server/TaskHandler/TaskFinishedHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
*/
interface TaskFinishedHandlerInterface
{
/**
* @param mixed $data
*/
public function handle(Server $server, int $taskId, $data): void;
}
3 changes: 3 additions & 0 deletions src/Server/TaskHandler/TaskHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@

interface TaskHandlerInterface
{
/**
* @param mixed $data
*/
public function handle(Server $server, int $taskId, int $fromId, $data): void;
}
4 changes: 2 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ function format_bytes(int $bytes): string
/**
* Simple decodes string of values as array.
*
* @param string $separator set separator
* @param array $stripChars characters to be stripped out from string
* @param string $separator set separator
* @param array<string> $stripChars characters to be stripped out from string
*
* @return string[]
*/
Expand Down

0 comments on commit b683a8d

Please sign in to comment.