Skip to content

Commit

Permalink
Fix errors reported by static analyzers
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Apr 14, 2024
1 parent dbb3d46 commit 5b3bfe3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="psalm" version="5.20.0" installed="5.20.0" location="./tools/psalm" copy="false"/>
<phar name="psalm" version="5.23.1" installed="5.23.1" location="./tools/psalm" copy="false"/>
</phive>
113 changes: 58 additions & 55 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.20.0@3f284e96c9d9be6fe6b15c79416e1d1903dcfef4">
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
<file src="src/Cache/Engine/FileEngine.php">
<TooManyTemplateParams>
<code>$iterator</code>
<code><![CDATA[$iterator]]></code>
</TooManyTemplateParams>
</file>
<file src="src/Cache/Engine/RedisEngine.php">
Expand All @@ -15,93 +15,93 @@
<InvalidReturnStatement>
<code><![CDATA[$this->_Redis->set($key, $value)]]></code>
<code><![CDATA[$this->_Redis->setEx($key, $duration, $value)]]></code>
<code>$value</code>
<code>$value</code>
<code><![CDATA[$value]]></code>
<code><![CDATA[$value]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code>bool</code>
<code>int|false</code>
<code>int|false</code>
<code><![CDATA[bool]]></code>
<code><![CDATA[int|false]]></code>
<code><![CDATA[int|false]]></code>
</InvalidReturnType>
</file>
<file src="src/Core/PluginConfig.php">
<RedundantCondition>
<code>is_array($pluginLoadConfig)</code>
<code><![CDATA[is_array($pluginLoadConfig)]]></code>
</RedundantCondition>
</file>
<file src="src/Event/EventDispatcherTrait.php">
<MoreSpecificImplementedParamType>
<code>$subject</code>
<code><![CDATA[$subject]]></code>
</MoreSpecificImplementedParamType>
</file>
<file src="src/Event/EventManager.php">
<InvalidArgument>
<code>_callListener</code>
<code>addEventToList</code>
<code>addEventToList</code>
<code><![CDATA[_callListener]]></code>
<code><![CDATA[addEventToList]]></code>
<code><![CDATA[addEventToList]]></code>
</InvalidArgument>
<InvalidReturnStatement>
<code>$event</code>
<code>$event</code>
<code><![CDATA[$event]]></code>
<code><![CDATA[$event]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code>EventInterface</code>
<code><![CDATA[EventInterface]]></code>
</InvalidReturnType>
</file>
<file src="src/I18n/Date.php">
<ImpureFunctionCall>
<code>call_user_func(static::$_jsonEncodeFormat, $this)</code>
<code>static::$_jsonEncodeFormat</code>
<code><![CDATA[call_user_func(static::$_jsonEncodeFormat, $this)]]></code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
</ImpureFunctionCall>
<ImpureMethodCall>
<code>_formatObject</code>
<code>dateAgoInWords</code>
<code>diffFormatter</code>
<code>getDefaultLocale</code>
<code><![CDATA[_formatObject]]></code>
<code><![CDATA[dateAgoInWords]]></code>
<code><![CDATA[diffFormatter]]></code>
<code><![CDATA[getDefaultLocale]]></code>
</ImpureMethodCall>
<ImpureStaticProperty>
<code>static::$_jsonEncodeFormat</code>
<code>static::$_jsonEncodeFormat</code>
<code>static::$_jsonEncodeFormat</code>
<code>static::$_toStringFormat</code>
<code>static::$niceFormat</code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
<code><![CDATA[static::$_toStringFormat]]></code>
<code><![CDATA[static::$niceFormat]]></code>
</ImpureStaticProperty>
</file>
<file src="src/I18n/DateTime.php">
<ImpureFunctionCall>
<code>call_user_func(static::$_jsonEncodeFormat, $this)</code>
<code>static::$_jsonEncodeFormat</code>
<code><![CDATA[call_user_func(static::$_jsonEncodeFormat, $this)]]></code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
</ImpureFunctionCall>
<ImpureMethodCall>
<code>_formatObject</code>
<code>diffFormatter</code>
<code>getDefaultLocale</code>
<code>timeAgoInWords</code>
<code><![CDATA[_formatObject]]></code>
<code><![CDATA[diffFormatter]]></code>
<code><![CDATA[getDefaultLocale]]></code>
<code><![CDATA[timeAgoInWords]]></code>
</ImpureMethodCall>
<ImpureStaticProperty>
<code>static::$_jsonEncodeFormat</code>
<code>static::$_jsonEncodeFormat</code>
<code>static::$_jsonEncodeFormat</code>
<code>static::$_toStringFormat</code>
<code>static::$niceFormat</code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
<code><![CDATA[static::$_toStringFormat]]></code>
<code><![CDATA[static::$niceFormat]]></code>
</ImpureStaticProperty>
</file>
<file src="src/I18n/Time.php">
<ImpureFunctionCall>
<code>call_user_func(static::$_jsonEncodeFormat, $this)</code>
<code>static::$_jsonEncodeFormat</code>
<code><![CDATA[call_user_func(static::$_jsonEncodeFormat, $this)]]></code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
</ImpureFunctionCall>
<ImpureMethodCall>
<code>_formatObject</code>
<code>getDefaultLocale</code>
<code>toNative</code>
<code><![CDATA[_formatObject]]></code>
<code><![CDATA[getDefaultLocale]]></code>
<code><![CDATA[toNative]]></code>
</ImpureMethodCall>
<ImpureStaticProperty>
<code>static::$_jsonEncodeFormat</code>
<code>static::$_jsonEncodeFormat</code>
<code>static::$_jsonEncodeFormat</code>
<code>static::$_toStringFormat</code>
<code>static::$niceFormat</code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
<code><![CDATA[static::$_jsonEncodeFormat]]></code>
<code><![CDATA[static::$_toStringFormat]]></code>
<code><![CDATA[static::$niceFormat]]></code>
</ImpureStaticProperty>
</file>
<file src="src/TestSuite/Constraint/EventFired.php">
Expand Down Expand Up @@ -148,27 +148,30 @@
</file>
<file src="src/TestSuite/Constraint/Session/FlashParamEquals.php">
<InternalClass>
<code>new AssertionFailedError($message)</code>
<code><![CDATA[new AssertionFailedError($message)]]></code>
</InternalClass>
<InternalMethod>
<code>new AssertionFailedError($message)</code>
<code><![CDATA[new AssertionFailedError($message)]]></code>
</InternalMethod>
</file>
<file src="src/TestSuite/TestCase.php">
<UndefinedVariable>
<code>$previousHandler</code>
<code><![CDATA[$previousHandler]]></code>
</UndefinedVariable>
</file>
<file src="src/Utility/Filesystem.php">
<TooManyTemplateParams>
<code>$iterator</code>
<code>$iterator</code>
<code><![CDATA[$iterator]]></code>
<code><![CDATA[$iterator]]></code>
</TooManyTemplateParams>
</file>
<file src="src/Utility/Hash.php">
<RedundantCondition>
<code>is_array($_list)</code>
</RedundantCondition>
<InvalidReturnStatement>
<code><![CDATA[$data]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code><![CDATA[(T is array ? array : \ArrayAccess)]]></code>
</InvalidReturnType>
</file>
<file src="src/Validation/Validation.php">
<RedundantCondition>
Expand Down
13 changes: 11 additions & 2 deletions src/Utility/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace Cake\Utility;

use ArrayAccess;
use Cake\Core\Exception\CakeException;
use InvalidArgumentException;
use const SORT_ASC;
use const SORT_DESC;
Expand Down Expand Up @@ -336,11 +337,13 @@ public static function insert(array $data, string $path, mixed $values = null):
/**
* Perform a simple insert/remove operation.
*
* @template T of \ArrayAccess|array
* @param string $op The operation to do.
* @param \ArrayAccess|array $data The data to operate on.
* @param T $data The data to operate on.
* @param list<string> $path The path to work on.
* @param mixed $values The values to insert when doing inserts.
* @return \ArrayAccess|array data.
* @psalm-return (T is array ? array : \ArrayAccess)
*/
protected static function _simpleOp(
string $op,
Expand Down Expand Up @@ -387,9 +390,11 @@ protected static function _simpleOp(
* You can use `{n}` and `{s}` to remove multiple elements
* from $data.
*
* @param array \ArrayAccess|$data The data to operate on
* @template T of \ArrayAccess|array
* @param T $data The data to operate on
* @param string $path A path expression to use to remove.
* @return \ArrayAccess|array The modified array.
* @psalm-return (T is array ? array : \ArrayAccess)
* @link https://book.cakephp.org/5/en/core-libraries/hash.html#Cake\Utility\Hash::remove
*/
public static function remove(ArrayAccess|array $data, string $path): ArrayAccess|array
Expand All @@ -415,6 +420,10 @@ public static function remove(ArrayAccess|array $data, string $path): ArrayAcces

[$token, $conditions] = self::_splitConditions($token);

if (!is_iterable($data)) {
throw new CakeException('Cannot use path tokens of type `{}` or `[]` for non-iterable objects.');
}

foreach ($data as $k => $v) {
$match = static::_matchToken($k, $token);
if ($match && (is_array($v) || $v instanceof ArrayAccess)) {
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Utility/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2205,6 +2205,23 @@ public function testRemoveArrayAccess(): void
'vars' => [],
];
$this->assertSame($expected, $result->getArrayCopy());

$a = new ArrayObject([
0 => [
'name' => 'pages',
],
1 => [
'name' => 'files',
],
]);

$result = Hash::remove($a, '{n}[name=files]');
$expected = [
0 => [
'name' => 'pages',
],
];
$this->assertSame($expected, $result->getArrayCopy());
}

/**
Expand Down

0 comments on commit 5b3bfe3

Please sign in to comment.