Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parameters:
treatPhpDocTypesAsCertain: false
level: 5
level: 6
paths:
- src
- test
Expand Down
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Php82\Rector\Class_\ReadOnlyClassRector;

return RectorConfig::configure()
Expand All @@ -22,6 +23,10 @@
->withSkip([
// Don't make whole classes read-only in libraries, it makes BC awkward
ReadOnlyClassRector::class,
// Don't force test properties to readonly
ReadOnlyPropertyRector::class => [
__DIR__.'/test/unit/Object/ObjectPropertyPopulatorTest.php',
],
])
->withImportNames(
removeUnusedImports: true,
Expand Down
23 changes: 18 additions & 5 deletions src/ArrayHelpers/AssociativeArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class AssociativeArrayUtils
*
* Updated PHP7 implementation of Kohana's \Arr::merge, with the same behaviour
*
* @param array $merge_base initial array
* @param array $merge_patches,... array to merge
* @param array<mixed> $merge_base initial array
* @param array<mixed> ...$merge_patches array to merge
*
* @return array<mixed>
*/
public static function deepMerge(array $merge_base, array ...$merge_patches): array
{
Expand Down Expand Up @@ -74,7 +76,7 @@ public static function deepMerge(array $merge_base, array ...$merge_patches): ar
*
* Imported from the Kohana framework.
*
* @param array $array array to check
* @param array<mixed> $array array to check
*
* @author Kohana Team
*/
Expand All @@ -91,6 +93,11 @@ public static function isAssociative(array $array): bool
/**
* Turns 'this.thing.here => bar' into ['this']['thing']['here'] = bar.
*/
/**
* @param array<string, mixed> $input
*
* @return array<mixed>
*/
public static function pathsToNested(array $input): array
{
$result = [];
Expand All @@ -104,8 +111,8 @@ public static function pathsToNested(array $input): array
/**
* Set a value on an array by path.
*
* @param array $array Array to update
* @param string|array $path Path
* @param array<mixed> $array Array to update
* @param string|list<string> $path Path
* @param mixed $value Value to set
* @param string $delimiter Path delimiter
*
Expand Down Expand Up @@ -148,6 +155,12 @@ public static function setPath(array &$array, string|array $path, mixed $value,
* assert($array === ['mix' => 'of', 'useful' => 'separately']);
* assert($result === ['things' => 'that', 'might' => 'be']); // Note `undefined` not present in source or result
*/
/**
* @param array<array-key, mixed> $source
* @param list<array-key> $pop_keys
*
* @return array<array-key, mixed>
*/
public static function popKeys(array &$source, array $pop_keys): array
{
$result = [];
Expand Down
4 changes: 4 additions & 0 deletions src/ArrayHelpers/UniqueMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*
* Note that you can still explicitly access it like `$foo = $map['undefined'] ?? 'default'` if
* you want to access a key that may or may not exist (just like a standard array).
*
* @implements ArrayAccess<string, mixed>
* @implements IteratorAggregate<string, mixed>
*/
class UniqueMap implements ArrayAccess, IteratorAggregate, Countable
{
Expand Down Expand Up @@ -59,6 +62,7 @@ public function offsetUnset(mixed $offset): void
unset($this->items[$offset]);
}

/** @return ArrayIterator<string, mixed> */
public function getIterator(): Traversable
{
return new ArrayIterator($this->items);
Expand Down
3 changes: 3 additions & 0 deletions src/CSV/CSVWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class CSVWriter

protected bool $owns_resource;

/** @var list<array-key>|null */
protected ?array $expect_schema = null;
Comment on lines +35 to 36

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd kind-of expect the CSV to have string keys for the column headers, but arguably it could validly output numbered columns if the incoming array was a list / integer keys. So it's correct that actually this can be any array-key.


/** @var array{write_utf8_bom: bool, quote_headers: bool, eol: string} */
protected array $options = [
'write_utf8_bom' => false,
'quote_headers' => true,
Expand Down Expand Up @@ -61,6 +63,7 @@ public function open(mixed $file, array $options = []): void
$this->options = array_merge($this->options, $options);
}

/** @param array<array-key, mixed> $row */
public function write(array $row): void
{
if (!$this->isResourceOpen()) {
Expand Down
3 changes: 3 additions & 0 deletions src/CSV/MismatchedSchemaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

final class MismatchedSchemaException extends InvalidArgumentException
{
/** @param list<array-key> $expected
* @param list<array-key> $actual
*/
public static function forSchema(array $expected, array $actual): self
{
return new self(
Expand Down
8 changes: 6 additions & 2 deletions src/Cookie/ArrayCookieWrapperStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

class ArrayCookieWrapperStub extends CookieWrapper
{
/** @var array<string, list<array{value: ?string, opts: array<string, mixed>}>> */
protected $set_cookie_calls = [];

/** @param array<string, mixed> $cookies */
public function __construct(array $cookies = [])
{
parent::__construct(has_ssl_available: false);
Expand All @@ -18,16 +20,18 @@ public function set(string $name, ?string $value = null, array $options = []): v
$this->set_cookie_calls[$name][] = ['value' => $value, 'opts' => $options];
}

protected function set_cookie($name, $value, $options): void
/** @param array<string, mixed> $options */
protected function set_cookie(string $name, ?string $value, array $options): void
{
// No-op
}

protected function headers_sent(&$file, &$line): bool
protected function headers_sent(?string &$file = null, ?int &$line = null): bool
{
return false;
}

/** @return array<string, list<array{value: ?string, opts: array<string, mixed>}>> */
public function inspectSetCookies(): array
{
return $this->set_cookie_calls;
Expand Down
5 changes: 3 additions & 2 deletions src/Cookie/CookieWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ public function delete(string $name): void
unset($this->cookies[$name]);
}

protected function set_cookie($name, $value, $options)
/** @param array<string, mixed> $options */
protected function set_cookie(string $name, ?string $value, array $options): void
{
setcookie($name, $value, $options);
}

protected function headers_sent(&$file, &$line): bool
protected function headers_sent(?string &$file = null, ?int &$line = null): bool
{
return headers_sent($file, $line);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cookie/HeadersSentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class HeadersSentException extends LogicException
{
public function __construct(string $cookie_name, string $output_file, string $output_line)
public function __construct(string $cookie_name, string $output_file, ?int $output_line)
{
parent::__construct(
"Cannot assign cookie $cookie_name - headers have been sent by output at $output_file:$output_line"
Expand Down
2 changes: 1 addition & 1 deletion src/DateTime/Clock/RealtimeClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getMicrotime(): float
return microtime(true);
}

public function usleep($microseconds): void
public function usleep(int $microseconds): void
{
usleep($microseconds);
}
Expand Down
11 changes: 5 additions & 6 deletions src/DateTime/Clock/StoppedMockClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ final class StoppedMockClock extends RealtimeClock
{
private float $current_microtime;

/**
* @var int[]
*/
/** @var list<int> */
private array $sleeps = [];

/**
Expand Down Expand Up @@ -78,18 +76,19 @@ public function tickMicroseconds(float $microseconds): void
$this->current_microtime += ($microseconds / 1000000);
}

public function usleep($microseconds): void
public function usleep(int $microseconds): void
{
$this->tickMicroseconds($microseconds);
$this->sleeps[] = $microseconds;
}

public function assertSlept(array $expected, $msg = ''): void
/** @param list<int> $expected */
public function assertSlept(array $expected, string $msg = ''): void
{
Assert::assertSame($expected, $this->sleeps, $msg);
}

public function assertNeverSlept($msg = ''): void
public function assertNeverSlept(string $msg = ''): void
{
Assert::assertSame([], $this->sleeps, $msg);
}
Expand Down
10 changes: 9 additions & 1 deletion src/DeploymentConfig/ConfigMapDeclarationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ConfigMapDeclarationParser
*
* This can then be used as a simple lookup table in the deployment config class
*/
/**
* @param array<mixed> $declarations
*
* @return array<string, mixed>
*/
public function parse(array $declarations): array
{
$result = [];
Expand All @@ -38,7 +43,10 @@ public function parse(array $declarations): array
return $result;
}

protected function validateDeclaration($declaration): array
/**
* @return array{list<string>, mixed}
*/
protected function validateDeclaration(mixed $declaration): array
{
if (!is_array($declaration)) {
throw InvalidConfigException::invalidMapDeclaration('not an array');
Expand Down
2 changes: 1 addition & 1 deletion src/DeploymentConfig/ConfigValueDecrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(protected string $keypair_dir)
{
}

public function decrypt($value)
public function decrypt(mixed $value): mixed
{
if (is_string($value) && preg_match('/^#SECRET-?(?P<keypair>.*)#(?P<ciphertext>.+)$/', $value, $matches)) {
return $this->tryDecrypt($matches['ciphertext'], $this->getKeypair($matches['keypair'] ?: 'default'));
Expand Down
7 changes: 4 additions & 3 deletions src/DeploymentConfig/DeploymentConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function instance()
}

/**
* @param array|null $env_vars - if missing will default to reading from $_SERVER
* @param array<string, mixed>|null $env_vars - if missing will default to reading from $_SERVER
*/
private function __construct(
?array $env_vars = null,
Expand Down Expand Up @@ -91,7 +91,7 @@ public function read(string $path): ?string
/**
* Use this when the value of a config / secret is type-sensitive (integer / boolean / supports null value).
*/
public function readJSON(string $string)
public function readJSON(string $string): mixed
{
$value = $this->read($string);
if ($value === null) {
Expand All @@ -105,7 +105,8 @@ public function readJSON(string $string)
}
}

public function map(array ...$declarations)
/** @param array<mixed> ...$declarations */
public function map(array ...$declarations): mixed
{
$map = $this->map_parser->parse($declarations);
if (array_key_exists($this->environment, $map)) {
Expand Down
2 changes: 1 addition & 1 deletion src/DeploymentConfig/InvalidConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function corruptKeypair(string $keypair_file, string $reason): Inv
return new self('Corrupt config keypair file '.$keypair_file.' ('.$reason.')');
}

public static function duplicateEnvironmentMap($env): InvalidConfigException
public static function duplicateEnvironmentMap(string $env): InvalidConfigException
{
return new self('Duplicate environment mapping for '.$env);
}
Expand Down
4 changes: 2 additions & 2 deletions src/DeploymentConfig/MissingConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

final class MissingConfigException extends RuntimeException
{
public static function missingFile($path, $real_path, $env): static
public static function missingFile(string $path, string $real_path, string $env): static
{
return new self("Config `$path` is not present in `$real_path` and is required for `$env` environment");
}

public static function missingMapValue($env): static
public static function missingMapValue(string $env): static
{
return new self("No config map entry for `$env`, nor one for `*`");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Logging/DefaultLogMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static function requestTrace(array $server = [], ?string $gcp_project = n
*
* If the file does not exist or cannot be read the version will be returned as '#ERROR#'
*
* @return array|array[]
* @return array{serviceContext: array{service: string, version: mixed}}
*/
public static function serviceContext(string $service_name, ?string $version_file_path = null): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Logging/DeviceIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function init(): void
}
}

protected function assignNewIdAndSetCookies()
protected function assignNewIdAndSetCookies(): void
{
$this->id = Base64Url::encode(random_bytes(16));
$expire = (new DateTimeImmutable())->add(new DateInterval(static::COOKIE_LIFETIME));
Expand Down
7 changes: 7 additions & 0 deletions src/Logging/ExternalCallSiteFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class ExternalCallSiteFinder
* }
* }
*/
/**
* @param list<array<string, mixed>> $trace
* @param list<class-string> $ignore_classes
*
* @return array<string, mixed>
*/
public function findExternalCall(array $trace, array $ignore_classes): array
{
$orig_trace = $trace;
Expand Down Expand Up @@ -102,6 +108,7 @@ public function findExternalCall(array $trace, array $ignore_classes): array
];
}

/** @param list<class-string> $ignore_classes */
protected function isIgnoredClass(string $class, array $ignore_classes): bool
{
foreach ($ignore_classes as $ignored_class) {
Expand Down
2 changes: 2 additions & 0 deletions src/Logging/LogMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface LogMetadataProvider
* if appropriate return a warning value in your array. Only unexpected / system-level exceptions (failure to
* connect to external service / invalid configuration even when fully bootstrapped / etc) should bubble to allow
* these to be detected and fixed.
*
* @return array<string, mixed>
*/
public function getMetadata(): array;
}
Loading
Loading