Skip to content

Commit

Permalink
Update to use PHP 8.1 syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Abdul Malik Ikhsan <[email protected]>
  • Loading branch information
samsonasik committed Nov 3, 2024
1 parent 1c3fa63 commit 5c21c32
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/CountryCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
final class CountryCode
{
/** @param non-empty-string $code */
private function __construct(private string $code)
private function __construct(private readonly string $code)
{
}

Expand Down
4 changes: 1 addition & 3 deletions src/Geography/DefaultCountryCodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,7 @@ public function count(): int
public function toArray(): array
{
return array_map(
static function (string $code): CountryCode {
return CountryCode::fromString($code);
},
static fn(string $code): CountryCode => CountryCode::fromString($code),
self::LIST
);
}
Expand Down
6 changes: 1 addition & 5 deletions src/Translator/Loader/PhpMemoryArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
*/
class PhpMemoryArray implements RemoteLoaderInterface
{
/** @var array */
protected $messages;

/** @param array $messages */
public function __construct($messages)
public function __construct(protected $messages)
{
$this->messages = $messages;
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Translator/LoaderPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;

use function gettype;
use function is_object;
use function get_debug_type;
use function sprintf;

/**
Expand Down Expand Up @@ -112,7 +111,7 @@ public function validate($plugin)

throw new InvalidServiceException(sprintf(
'Plugin of type %s is invalid; must implement %s or %s',
is_object($plugin) ? $plugin::class : gettype($plugin),
get_debug_type($plugin),
FileLoaderInterface::class,
RemoteLoaderInterface::class
));
Expand All @@ -134,10 +133,10 @@ public function validatePlugin($plugin)
{
try {
$this->validate($plugin);
} catch (InvalidServiceException $e) {
} catch (InvalidServiceException) {
throw new Exception\RuntimeException(sprintf(
'Plugin of type %s is invalid; must implement %s or %s',
is_object($plugin) ? $plugin::class : gettype($plugin),
get_debug_type($plugin),
FileLoaderInterface::class,
RemoteLoaderInterface::class
));
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/LoaderPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LoaderPluginManagerFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $name, ?array $options = null)
{
$options = $options ?? [];
$options ??= [];
$pluginManager = new LoaderPluginManager($container, $options);

// If this is in a laminas-mvc application, the ServiceListener will inject
Expand Down
19 changes: 8 additions & 11 deletions src/Translator/Plural/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,20 @@ class Rule
*/
protected $ast;

/**
* Number of plurals in this rule.
*
* @var int
*/
protected $numPlurals;

/**
* Create a new plural rule.
*
* @param int $numPlurals
* @param array $ast
*/
protected function __construct($numPlurals, array $ast)
{
$this->numPlurals = $numPlurals;
$this->ast = $ast;
protected function __construct(
/**
* Number of plurals in this rule.
*/
protected $numPlurals,
array $ast
) {
$this->ast = $ast;
}

/**
Expand Down
31 changes: 12 additions & 19 deletions src/Translator/Plural/Symbol.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ class Symbol
*/
public $parser;

/**
* Node or token type name.
*
* @var string
*/
public $id;

/**
* Left binding power (precedence).
*
* @var int
*/
public $leftBindingPower;

/**
* Getter for null denotation.
*
Expand Down Expand Up @@ -86,11 +72,18 @@ class Symbol
* @param string $id
* @param int $leftBindingPower
*/
public function __construct(Parser $parser, $id, $leftBindingPower)
{
$this->parser = $parser;
$this->id = $id;
$this->leftBindingPower = $leftBindingPower;
public function __construct(
Parser $parser,
/**
* Node or token type name.
*/
public $id,
/**
* Left binding power (precedence).
*/
public $leftBindingPower
) {
$this->parser = $parser;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public function getPluginManager()
public function translate($message, $textDomain = 'default', $locale = null)
{
$locale = $locale === '' ? null : $locale;
$locale = $locale ?? $this->getLocale();
$locale ??= $this->getLocale();
$translation = $this->getTranslatedMessage($message, $locale, $textDomain);

if ($translation !== null && $translation !== '') {
Expand Down Expand Up @@ -381,7 +381,7 @@ public function translatePlural(
$textDomain = 'default',
$locale = null
) {
$locale = $locale ?? $this->getLocale();
$locale ??= $this->getLocale();
$translation = $this->getTranslatedMessage($singular, $locale, $textDomain);

if (is_string($translation)) {
Expand Down Expand Up @@ -493,7 +493,7 @@ public function addTranslationFile(
$textDomain = 'default',
$locale = null
) {
$locale = $locale ?? '*';
$locale ??= '*';

if (! isset($this->files[$textDomain])) {
$this->files[$textDomain] = [];
Expand Down Expand Up @@ -754,7 +754,7 @@ protected function loadMessagesFromFiles($textDomain, $locale)
*/
public function getAllMessages($textDomain = 'default', $locale = null)
{
$locale = $locale ?? $this->getLocale();
$locale ??= $this->getLocale();

if (! isset($this->messages[$textDomain][$locale])) {
$this->loadMessages($textDomain, $locale);
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public function isValid($value)
$this->invalidateFormatter = true;
return false;
}
} catch (IntlException $intlException) {
} catch (IntlException) {
$this->error(self::INVALID_DATETIME);
$this->invalidateFormatter = true;
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/IsInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function isValid($value)
$this->error(self::NOT_INT);
return false;
}
} catch (IntlException $intlException) {
} catch (IntlException) {
$this->error(self::NOT_INT);
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Validator/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use function is_scalar;
use function is_string;
use function preg_match;
use function str_starts_with;
use function strlen;
use function strpos;
use function strtoupper;
use function substr;

Expand Down Expand Up @@ -233,11 +233,11 @@ public function isValid($value = null, $context = null)
* 3) Bare country prefix
*/
$valueNoCountry = null;
if (0 === strpos((string) $value, '+' . $countryPattern['code'])) {
if (str_starts_with((string) $value, '+' . $countryPattern['code'])) {
$valueNoCountry = substr((string) $value, $codeLength + 1);
} elseif (0 === strpos((string) $value, '00' . $countryPattern['code'])) {
} elseif (str_starts_with((string) $value, '00' . $countryPattern['code'])) {
$valueNoCountry = substr((string) $value, $codeLength + 2);
} elseif (0 === strpos((string) $value, $countryPattern['code'])) {
} elseif (str_starts_with((string) $value, $countryPattern['code'])) {
$valueNoCountry = substr((string) $value, $codeLength);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Validator/PostCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public function isValid($value)
$this->error(self::SERVICE, $value);
return false;
}
} catch (\Exception $e) {
} catch (\Exception) {
$this->error(self::SERVICEFAILURE, $value);
return false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/View/Helper/CountryCodeDataList.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
final class CountryCodeDataList
{
public function __construct(
private CountryCodeListInterface $codeList,
private Escaper $escaper,
private string $defaultLocale
private readonly CountryCodeListInterface $codeList,
private readonly Escaper $escaper,
private readonly string $defaultLocale
) {
}

Expand All @@ -35,8 +35,8 @@ public function __construct(
*/
public function __invoke(?string $locale = null, array $dataListAttributes = []): string
{
$locale = $locale ?? $this->defaultLocale;
$list = array_map(function (CountryCode $code) use ($locale): string {
$locale ??= $this->defaultLocale;
$list = array_map(function (CountryCode $code) use ($locale): string {
$name = Locale::getDisplayRegion('-' . $code->toString(), $locale);
return sprintf(
'<option value="%s" label="%s">',
Expand Down

0 comments on commit 5c21c32

Please sign in to comment.