Skip to content

Commit

Permalink
Fix issues found by Psalm 5.22.x
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <george@net-glue.co.uk>
  • Loading branch information
gsteel committed Mar 1, 2024
1 parent 59ef134 commit 4c92861
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 265 deletions.
481 changes: 237 additions & 244 deletions psalm-baseline.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Filter/Alnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getAllowWhiteSpace()
* Returns $value as string with all non-alphanumeric characters removed
*
* @param mixed $value
* @return ($value is scalar|list<scalar> ? string|list<scalar> : mixed)
* @return string|list<string>|mixed
*/
public function filter($value)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Alpha extends Alnum
* Returns the string $value, removing all but alphabetic characters
*
* @param mixed $value
* @return ($value is scalar|list<scalar> ? string|list<scalar> : mixed)
* @return ($value is scalar ? string : ($value is list<scalar> ? list<string> : mixed))
*/
public function filter($value)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/Loader/AbstractFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function resolveFile($filename)
protected function resolveViaIncludePath($filename)
{
$resolvedIncludePath = stream_resolve_include_path($filename);
if (! $resolvedIncludePath || ! is_file($resolvedIncludePath) || ! is_readable($resolvedIncludePath)) {
if ($resolvedIncludePath === false || ! is_file($resolvedIncludePath) || ! is_readable($resolvedIncludePath)) {
return false;
}
return $resolvedIncludePath;
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/Loader/Gettext.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Gettext extends AbstractFileLoader
public function load($locale, $filename)
{
$resolvedFile = $this->resolveFile($filename);
if (! $resolvedFile) {
if ($resolvedFile === false) {
throw new Exception\InvalidArgumentException(sprintf(
'Could not find or open file %s for reading',
$filename
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/LoaderPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LoaderPluginManagerFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $name, ?array $options = null)
{
$options = $options ?: [];
$options = $options ?? [];
$pluginManager = new LoaderPluginManager($container, $options);

// If this is in a laminas-mvc application, the ServiceListener will inject
Expand Down
21 changes: 10 additions & 11 deletions src/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public function getPluginManager()
*/
public function translate($message, $textDomain = 'default', $locale = null)
{
$locale = $locale ?: $this->getLocale();
$locale = $locale ?? $this->getLocale();
$translation = $this->getTranslatedMessage($message, $locale, $textDomain);

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

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

if (! isset($this->files[$textDomain])) {
$this->files[$textDomain] = [];
Expand Down Expand Up @@ -604,12 +604,12 @@ protected function loadMessages($textDomain, $locale)
}
}

$messagesLoaded = false;
$messagesLoaded |= $this->loadMessagesFromRemote($textDomain, $locale);
$messagesLoaded |= $this->loadMessagesFromPatterns($textDomain, $locale);
$messagesLoaded |= $this->loadMessagesFromFiles($textDomain, $locale);
$messagesLoaded = 0;
$messagesLoaded |= (int) $this->loadMessagesFromRemote($textDomain, $locale);
$messagesLoaded |= (int) $this->loadMessagesFromPatterns($textDomain, $locale);
$messagesLoaded |= (int) $this->loadMessagesFromFiles($textDomain, $locale);

if (! $messagesLoaded) {
if ($messagesLoaded === 0) {
$discoveredTextDomain = null;
if ($this->isEventManagerEnabled()) {
$until = static fn($r): bool => $r instanceof TextDomain;
Expand All @@ -628,10 +628,9 @@ protected function loadMessages($textDomain, $locale)
}

$this->messages[$textDomain][$locale] = $discoveredTextDomain;
$messagesLoaded = true;
}

if ($messagesLoaded && $cache !== null) {
if ($cache !== null) {
$cache->setItem($cacheId, $this->messages[$textDomain][$locale]);
}
}
Expand Down Expand Up @@ -754,7 +753,7 @@ protected function loadMessagesFromFiles($textDomain, $locale)
*/
public function getAllMessages($textDomain = 'default', $locale = null)
{
$locale = $locale ?: $this->getLocale();
$locale = $locale ?? $this->getLocale();

if (! isset($this->messages[$textDomain][$locale])) {
$this->loadMessages($textDomain, $locale);
Expand Down
12 changes: 9 additions & 3 deletions src/Validator/IsFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ public function isValid($value)
$decSeparatorPosition = $this->wrapper->strpos($value, $decSeparator);

//We have separators, and they are flipped. i.e. 2.000,000 for en-US
if ($groupSeparatorPosition && $decSeparatorPosition && $groupSeparatorPosition > $decSeparatorPosition) {
if (
$groupSeparatorPosition !== false
&& $decSeparatorPosition !== false
&& $groupSeparatorPosition > $decSeparatorPosition
) {
$this->error(self::NOT_FLOAT);

return false;
Expand All @@ -195,7 +199,8 @@ public function isValid($value)
'/'
)
. ']{0,3}';
$suffix = $formatter->getTextAttribute(NumberFormatter::NEGATIVE_SUFFIX)
$suffix = $formatter->getTextAttribute(NumberFormatter::NEGATIVE_SUFFIX);
$suffix = $suffix !== false
? '['
. preg_quote(
$formatter->getTextAttribute(NumberFormatter::POSITIVE_SUFFIX)
Expand Down Expand Up @@ -238,7 +243,8 @@ public function isValid($value)

// No strrpos() in wrappers yet. ICU 4.x doesn't have grouping size for
// everything. ICU 52 has 3 for ALL locales.
$groupSize = $formatter->getAttribute(NumberFormatter::GROUPING_SIZE) ?: 3;
$groupSize = $formatter->getAttribute(NumberFormatter::GROUPING_SIZE);
$groupSize = $groupSize === false ? 3 : $groupSize;
assert(is_int($groupSize));
$lastStringGroup = $this->wrapper->strlen($value) > $groupSize ?
$this->wrapper->substr($value, 0 - $groupSize) :
Expand Down
4 changes: 2 additions & 2 deletions src/Validator/PostCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public function isValid($value)
$service = $this->getService();
$locale = $this->getLocale();
$format = $this->getFormat();
if ((null === $format || '' === $format) && ! empty($locale)) {
if (($format === null || $format === '') && $locale !== null) {
$region = Locale::getRegion($locale);
if ('' === $region) {
throw new Exception\InvalidArgumentException('Locale must contain a region');
Expand All @@ -365,7 +365,7 @@ public function isValid($value)
$format .= '$/';
}

if (! empty($service)) {
if ($service !== null) {
if (! is_callable($service)) {
throw new Exception\InvalidArgumentException('Invalid callback given');
}
Expand Down
3 changes: 3 additions & 0 deletions test/Filter/AlnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function testBasic(): void

foreach ($valuesExpected as $input => $expected) {
$actual = $this->filter->filter($input);
self::assertIsString($actual);
self::assertEquals($expected, $actual);
}
}
Expand Down Expand Up @@ -128,6 +129,7 @@ public function testAllowWhiteSpace(): void

foreach ($valuesExpected as $input => $expected) {
$actual = $this->filter->filter($input);
self::assertIsString($actual);
self::assertEquals($expected, $actual);
}
}
Expand All @@ -145,6 +147,7 @@ public function testFilterSupportArray(): void
];

$actual = $filter->filter(array_keys($values));
self::assertIsArray($actual);

self::assertEquals(array_values($values), $actual);
}
Expand Down

0 comments on commit 4c92861

Please sign in to comment.