Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply PHP 8.0 Syntax and constructor promotion #104

Merged
merged 4 commits into from
Oct 13, 2022
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
3 changes: 3 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
<rule ref="SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden">
<exclude name="SlevomatCodingStandard.Commenting.ForbiddenComments.CommentForbidden"/>
</rule>
<rule ref="WebimpressCodingStandard.Classes.NoNullValues.Invalid">
<exclude name="WebimpressCodingStandard.Classes.NoNullValues.Invalid"/>
</rule>
</ruleset>
6 changes: 1 addition & 5 deletions src/AbstractContainerCommandLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@
*/
abstract class AbstractContainerCommandLoader implements CommandLoaderInterface
{
private ContainerInterface $container;

/** @psalm-var array<string, string> */
private $commandMap;

/** @psalm-param array<string, string> $commandMap */
final public function __construct(ContainerInterface $container, array $commandMap)
final public function __construct(private ContainerInterface $container, array $commandMap)
{
$this->container = $container;

Assert::isMap($commandMap);
Assert::allString($commandMap);
$this->commandMap = $commandMap;
Expand Down
13 changes: 5 additions & 8 deletions src/ContainerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@
use function class_exists;
use function file_exists;
use function sprintf;
use function strpos;
use function str_contains;

/**
* @internal
*/
final class ContainerResolver
{
/** @psalm-var non-empty-string */
private string $projectRoot;

/**
* @psalm-param non-empty-string $projectRoot
*/
public function __construct(string $projectRoot)
{
$this->projectRoot = $projectRoot;
public function __construct(
private string $projectRoot
) {
}

/**
Expand Down Expand Up @@ -149,7 +146,7 @@ private function isAbsolutePath(string $pathToContainer): bool
return true;
}

if (strpos($pathToContainer, '://') !== false) {
if (str_contains($pathToContainer, '://')) {
return true;
}

Expand Down
24 changes: 10 additions & 14 deletions src/Input/AbstractInputParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,17 @@ abstract class AbstractInputParam implements InputParamInterface

private string $description = '';

/**
* Parameter name; must be set by class composing trait!
*/
private string $name;

private bool $required = false;

/** @var null|string|string[] */
private $shortcut;

public function __construct(string $name)
{
$this->name = $name;
private null|string|array $shortcut = null;

public function __construct(
/**
* Parameter name; must be set by class composing trait!
*/
private string $name
) {
}

/**
Expand Down Expand Up @@ -134,11 +132,10 @@ public function setRequiredFlag(bool $required): InputParamInterface
}

/**
* @param mixed $shortcut
* @throws InvalidArgumentException When shortcut is an invalid type.
* @throws InvalidArgumentException When shortcut is empty.
*/
private function validateShortcut($shortcut): void
private function validateShortcut(mixed $shortcut): void
{
if (null === $shortcut) {
return;
Expand All @@ -163,8 +160,7 @@ private function validateShortcut($shortcut): void

array_walk(
$shortcut,
/** @param mixed $shortcut */
static function ($shortcut) {
static function (mixed $shortcut) {
Assert::stringNotEmpty($shortcut, sprintf(
'Only non-empty strings are allowed as shortcut names; received "%s"',
get_debug_type($shortcut)
Expand Down
41 changes: 11 additions & 30 deletions src/Input/AbstractParamAwareInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,15 @@
*/
abstract class AbstractParamAwareInput implements ParamAwareInputInterface
{
/** @var QuestionHelper */
protected $helper;

/** @var InputInterface */
protected $input;

/** @var OutputInterface */
protected $output;

/** @var array<string, InputParamInterface> */
private array $params;

/**
* @param array<string, InputParamInterface> $params
*/
public function __construct(InputInterface $input, OutputInterface $output, QuestionHelper $helper, array $params)
{
$this->input = $input;
$this->output = $output;
$this->helper = $helper;
$this->params = $params;
public function __construct(
protected InputInterface $input,
protected OutputInterface $output,
protected QuestionHelper $helper,
private array $params
) {
}

/**
Expand Down Expand Up @@ -182,10 +170,7 @@ public function getStream()
return $this->input->getStream();
}

/**
* @param mixed $value
*/
private function isParamValueProvided(InputParamInterface $param, $value): bool
private function isParamValueProvided(InputParamInterface $param, mixed $value): bool
{
$mode = $param->getOptionMode();

Expand All @@ -197,10 +182,9 @@ private function isParamValueProvided(InputParamInterface $param, $value): bool
}

/**
* @param mixed $value
* @return mixed
*/
private function normalizeValue($value, ?callable $normalizer)
private function normalizeValue(mixed $value, ?callable $normalizer)
{
// No normalizer: nothing to do
if ($normalizer === null) {
Expand All @@ -217,12 +201,11 @@ private function normalizeValue($value, ?callable $normalizer)
}

/**
* @param mixed $value
* @throws InvalidArgumentException When an array value is expected, but not
* provided.
*/
private function validateValue(
$value,
mixed $value,
bool $valueIsArray,
?callable $validator,
string $paramName
Expand Down Expand Up @@ -279,10 +262,9 @@ private function askQuestion(Question $question, bool $valueIsArray, bool $value
if ($valueIsRequired && [] === $values) {
$question->setValidator(
/**
* @param mixed $value
* @return mixed
*/
static function ($value) use ($validator) {
static function (mixed $value) use ($validator) {
if (null === $value || '' === $value) {
return $value;
}
Expand Down Expand Up @@ -311,10 +293,9 @@ private function prependSkipValidator(Question $question): ?callable

$question->setValidator(
/**
* @param mixed $value
* @return mixed
*/
static function ($value) use ($originalValidator) {
static function (mixed $value) use ($originalValidator) {
if ($value === null) {
return null;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Input/ChoiceParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ final class ChoiceParam extends AbstractInputParam
{
use AllowMultipleTrait;

private array $haystack;

/**
* @param array $haystack Choices to choose from.
*/
public function __construct(string $name, array $haystack)
public function __construct(string $name, private array $haystack)
{
parent::__construct($name);
$this->haystack = $haystack;
}

public function getQuestion(): Question
Expand Down
6 changes: 2 additions & 4 deletions src/Input/IntParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ public function getQuestion(): Question

$question->setNormalizer(
/**
* @param mixed $value
* @return mixed
*/
static function ($value) {
static function (mixed $value) {
if (is_numeric($value) && (string) (int) $value === $value) {
return (int) $value;
}
Expand All @@ -41,8 +40,7 @@ static function ($value) {
$min = $this->min;
$max = $this->max;
$question->setValidator(
/** @param mixed $value */
static function ($value) use ($min, $max): int {
static function (mixed $value) use ($min, $max): int {
Assert::integer($value, sprintf('Invalid value: integer expected, %s given', get_debug_type($value)));

if ($min !== null) {
Expand Down
13 changes: 5 additions & 8 deletions src/Input/Mapper/ArrayInputMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@

use function is_array;
use function ltrim;
use function strpos;
use function str_starts_with;

final class ArrayInputMapper implements InputMapperInterface
{
/** @psalm-var array<string|int, string|array<string, string>> */
private $map;

/**
* @psalm-param array<string|int, string|array<string, string>> $map
*/
public function __construct(array $map)
{
$this->map = $map;
public function __construct(
private array $map
) {
}

public function __invoke(InputInterface $input): array
Expand All @@ -38,7 +35,7 @@ public function __invoke(InputInterface $input): array
/** @psalm-suppress MixedAssignment The return value of `InputInterface#getOption`
* and `InputInterface#getArgument` is `mixed` and thus we have to assume it here as well.
*/
$params[$new] = strpos($old, '-') === 0
$params[$new] = str_starts_with($old, '-')
? $input->getOption(ltrim($old, '-'))
: $input->getArgument($old);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Input/PathParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public function getQuestion(): Question
$type = $this->type;

$question->setValidator(
/** @param mixed $value */
static function ($value) use ($mustExist, $type): string {
static function (mixed $value) use ($mustExist, $type): string {
Assert::string($value, sprintf('Invalid value: string expected, %s given', get_debug_type($value)));

if (! $mustExist) {
Expand Down
3 changes: 1 addition & 2 deletions src/Input/StringParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function getQuestion(): Question
$pattern = $this->pattern;

$question->setValidator(
/** @param mixed $value */
static function ($value) use ($pattern): string {
static function (mixed $value) use ($pattern): string {
Assert::string($value, sprintf(
'Invalid value: string expected, %s given',
get_debug_type($value)
Expand Down
16 changes: 5 additions & 11 deletions src/Listener/TerminateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use function realpath;
use function rtrim;
use function sprintf;
use function strpos;
use function str_starts_with;
use function strtolower;

use const PHP_EOL;
Expand All @@ -47,11 +47,8 @@ final class TerminateListener

private const HOME_PATH_REGEX = '#^(~|\$HOME)#';

private array $config;

public function __construct(array $config)
public function __construct(private array $config)
{
$this->config = $config;
}

public function __invoke(ConsoleTerminateEvent $event): void
Expand Down Expand Up @@ -134,10 +131,7 @@ public function __invoke(ConsoleTerminateEvent $event): void
}
}

/**
* @param mixed $inputMapperSpec
*/
private function createInputMapper($inputMapperSpec, string $commandClass): InputMapperInterface
private function createInputMapper(mixed $inputMapperSpec, string $commandClass): InputMapperInterface
{
if (is_array($inputMapperSpec)) {
$this->validateInputMap($inputMapperSpec, $commandClass);
Expand Down Expand Up @@ -218,13 +212,13 @@ private function matchesApplicationClass(string $class, string $vendorDir): bool
));

$filename = $this->normalizePath($filename);
if (0 !== strpos($filename, $vendorDir)) {
if (! str_starts_with($filename, $vendorDir)) {
return true;
}

foreach (self::ALLOWED_VENDORS as $vendor) {
$path = $vendorDir . $vendor . '/';
if (0 === strpos($filename, $path)) {
if (str_starts_with($filename, $path)) {
// Matches a Laminas or Mezzio command name
return true;
}
Expand Down
12 changes: 4 additions & 8 deletions test/Command/ParamAwareCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@

class ParamAwareCommandTest extends TestCase
{
/** @var ParamAwareCommandStub74|ParamAwareCommandStub80 */
private $command;

/**
* @var QuestionHelper|MockObject
* @psalm-var QuestionHelper&MockObject
*/
private $questionHelper;
private ParamAwareCommandStub74|ParamAwareCommandStub80 $command;

/** @psalm-var QuestionHelper&MockObject */
private QuestionHelper|MockObject $questionHelper;

public function setUp(): void
{
Expand Down
6 changes: 2 additions & 4 deletions test/Input/AbstractInputParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ public function invalidShortcutValues(): iterable

/**
* @dataProvider invalidShortcutValues
* @param mixed $shortcut
*/
public function testSettingShortcutShouldRaiseExceptionForInvalidValues(
$shortcut,
mixed $shortcut,
string $expectedMesage = 'must be null, a non-zero-length string, or an array'
): void {
$this->expectException(InvalidArgumentException::class);
Expand All @@ -119,10 +118,9 @@ public function validShortcutValues(): iterable

/**
* @dataProvider validShortcutValues
* @param mixed $shortcut
* @psalm-param null|string|string[] $shortcut
*/
public function testAllowsSettingShortcutWithValidValues($shortcut): void
public function testAllowsSettingShortcutWithValidValues(mixed $shortcut): void
{
$this->param->setShortcut($shortcut);
$this->assertSame($shortcut, $this->param->getShortcut());
Expand Down
Loading