Skip to content

Commit

Permalink
qa: optimize some psalm settings
Browse files Browse the repository at this point in the history
Allow `MixedAssignment` issue (by suppressing it). This issue will probably removed in future versions of psalm anyways and it mostly makes no real sense or is not avoidable. For further information, see: vimeo/psalm#8776

Allow `RedundantCastGivenDocblockType` as at least the redis extension does not always provide proper typs. The `RedisException` as well as the `RedisClusterException` does not always provide an integer `code` and thus we have to cast it to ensure that this will always be an integer. Once the extension implemented native type-hints, a `RedundantCast` will be emitted and we are good to remove the cast when requiring the extension version which implemented native type-hints.

Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
  • Loading branch information
boesing committed Dec 16, 2022
1 parent 629d237 commit c27a362
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 78 deletions.
59 changes: 4 additions & 55 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.29.0@7ec5ffbd5f68ae03782d7fd33fff0c45a69f95b3">
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="src/Redis.php">
<InvalidArgument occurrences="4">
<code>$ttl</code>
Expand Down Expand Up @@ -39,14 +39,6 @@
<MixedArgumentTypeCoercion occurrences="1">
<code>$normalizedKeys</code>
</MixedArgumentTypeCoercion>
<MixedAssignment occurrences="6">
<code>$namespacedKeyValuePairs[$this-&gt;namespacePrefix . $normalizedKey]</code>
<code>$normalizedKey</code>
<code>$serializer</code>
<code>$serializer</code>
<code>$value</code>
<code>$value</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="2">
<code>RedisOptions</code>
<code>int|float</code>
Expand Down Expand Up @@ -145,8 +137,7 @@
<code>$resource['server']</code>
<code>$server</code>
</MixedArgument>
<MixedArrayAccess occurrences="37">
<code>$info['redis_version']</code>
<MixedArrayAccess occurrences="36">
<code>$resource['database']</code>
<code>$resource['initialized']</code>
<code>$resource['initialized']</code>
Expand Down Expand Up @@ -199,39 +190,6 @@
<MixedArrayOffset occurrences="1">
<code>$libOptions[$constValue]</code>
</MixedArrayOffset>
<MixedAssignment occurrences="31">
<code>$constValue</code>
<code>$info</code>
<code>$k</code>
<code>$key</code>
<code>$key</code>
<code>$libOptions[$constValue]</code>
<code>$redis</code>
<code>$redis</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource</code>
<code>$resource['version']</code>
<code>$resource['version']</code>
<code>$result[$key]</code>
<code>$server</code>
<code>$success</code>
<code>$success</code>
<code>$success</code>
<code>$success</code>
<code>$v</code>
<code>$value</code>
<code>$value</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="8">
<code>RedisResource</code>
<code>array</code>
Expand All @@ -242,17 +200,15 @@
<code>string</code>
<code>string|null</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="10">
<MixedMethodCall occurrences="8">
<code>auth</code>
<code>connect</code>
<code>connect</code>
<code>connect</code>
<code>getOption</code>
<code>getOption</code>
<code>info</code>
<code>pconnect</code>
<code>select</code>
<code>setOption</code>
</MixedMethodCall>
<MixedReturnStatement occurrences="8">
<code>$resource['database']</code>
Expand All @@ -270,18 +226,11 @@
<RedundantCast occurrences="1">
<code>(int) $server['port']</code>
</RedundantCast>
<RedundantCastGivenDocblockType occurrences="4">
<RedundantCastGivenDocblockType occurrences="3">
<code>(int) $database</code>
<code>(int) $key</code>
<code>(string) $id</code>
<code>(string) $persistentId</code>
</RedundantCastGivenDocblockType>
<ReservedWord occurrences="1">
<code>$info</code>
</ReservedWord>
<UndefinedMethod occurrences="1">
<code>$info</code>
</UndefinedMethod>
</file>
<file src="test/integration/Laminas/RedisTest.php">
<MixedArgument occurrences="2">
Expand Down
22 changes: 22 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
findUnusedPsalmSuppress="true"
>
<projectFiles>
<directory name="src"/>
Expand All @@ -26,6 +27,27 @@
<referencedMethod name="Laminas\Cache\Storage\Adapter\RedisClusterOptions::setResourceManager"/>
</errorLevel>
</InternalMethod>
<!--
Since these are usually suppressed anyway AND psalm is already thinking about removing this issue at all,
suppressing these might be a good idea. See https://github.com/vimeo/psalm/pull/8776 for more information.
-->
<MixedAssignment>
<errorLevel type="suppress">
<directory name="*"/>
</errorLevel>
</MixedAssignment>
<!--
Mark these redundant casts as info as some docblocks are not accurate about the return values.
The redis extension does not provide integer exception codes and thus, passing null as a code starts
being deprecated starting with PHP 8.1 and might trigger errors in PHP 9.0 and thus, lets allow
casts for docblock types. Psalm has also `RedundantCast` which is shown when a native type-hint is being
cast. i.e. such as property type or return type.
-->
<RedundantCastGivenDocblockType>
<errorLevel type="info">
<directory name="*"/>
</errorLevel>
</RedundantCastGivenDocblockType>
</issueHandlers>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
Expand Down
6 changes: 0 additions & 6 deletions src/RedisCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ protected function internalGetItem(&$normalizedKey, &$success = null, &$casToken
return null;
}

/** @psalm-suppress MixedAssignment */
$value = $casToken = $values[$normalizedKey];
$success = true;
return $value;
Expand All @@ -202,15 +201,13 @@ protected function internalGetItems(array &$normalizedKeys): array
}

$result = [];
/** @psalm-suppress MixedAssignment */
foreach ($resultsByIndex as $keyIndex => $value) {
$normalizedKey = $normalizedKeys[$keyIndex];
$namespacedKey = $namespacedKeys[$keyIndex];
if ($value === false && ! $this->isFalseReturnValuePersisted($redis, $namespacedKey)) {
continue;
}

/** @psalm-suppress MixedAssignment */
$result[$normalizedKey] = $value;
}

Expand Down Expand Up @@ -299,15 +296,13 @@ protected function internalSetItems(array &$normalizedKeyValuePairs): array
$ttl = (int) $this->getOptions()->getTtl();

$namespacedKeyValuePairs = [];
/** @psalm-suppress MixedAssignment */
foreach ($normalizedKeyValuePairs as $normalizedKey => $value) {
$namespacedKeyValuePairs[$this->createNamespacedKey((string) $normalizedKey)] = $value;
}

$successByKey = [];

try {
/** @psalm-suppress MixedAssignment */
foreach ($namespacedKeyValuePairs as $key => $value) {
if ($ttl) {
/**
Expand Down Expand Up @@ -443,7 +438,6 @@ private function clusterException(
*/
private function isFalseReturnValuePersisted(RedisClusterFromExtension $redis, string $key): bool
{
/** @psalm-suppress MixedAssignment */
$serializer = $this->getLibOption(RedisClusterFromExtension::OPT_SERIALIZER);
if ($serializer === RedisClusterFromExtension::SERIALIZER_NONE) {
return false;
Expand Down
12 changes: 0 additions & 12 deletions src/RedisClusterResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,10 @@ private function applyLibraryOptions(
RedisClusterFromExtension $resource,
array $options
): RedisClusterFromExtension {
/** @psalm-suppress MixedAssignment */
foreach ($options as $option => $value) {
/**
* @see https://github.com/phpredis/phpredis#setoption
*
* @psalm-suppress InvalidArgument
* @psalm-suppress MixedArgument
*/
$resource->setOption($option, $value);
Expand All @@ -157,11 +155,6 @@ private function mergeLibraryOptionsFromCluster(array $options, RedisClusterFrom
continue;
}

/**
* @see https://github.com/phpredis/phpredis#getoption
*
* @psalm-suppress InvalidArgument
*/
$options[$option] = $resource->getOption($option);
}

Expand All @@ -178,11 +171,6 @@ public function getLibOption(int $option)
return $this->libraryOptions[$option];
}

/**
* @see https://github.com/phpredis/phpredis#getoption
*
* @psalm-suppress InvalidArgument
*/
return $this->libraryOptions[$option] = $this->getResource()->getOption($option);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ final class RedisClusterWithPhpIgbinaryTest extends AbstractCacheItemPoolIntegra
protected function setUp(): void
{
parent::setUp();
/** @psalm-suppress MixedArrayAssignment */
$this->skippedTests['testHasItemReturnsFalseWhenDeferredItemIsExpired'] = sprintf(
'%s storage doesn\'t support driver deferred',
RedisCluster::class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ final class RedisClusterWithPhpSerializeTest extends AbstractCacheItemPoolIntegr
protected function setUp(): void
{
parent::setUp();
/** @psalm-suppress MixedArrayAssignment */
$this->skippedTests['testHasItemReturnsFalseWhenDeferredItemIsExpired'] = sprintf(
'%s storage doesn\'t support driver deferred',
RedisCluster::class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ final class RedisClusterWithoutSerializerTest extends AbstractCacheItemPoolInteg
protected function setUp(): void
{
parent::setUp();
/** @psalm-suppress MixedArrayAssignment */
$this->skippedTests['testHasItemReturnsFalseWhenDeferredItemIsExpired'] = sprintf(
'%s storage doesn\'t support driver deferred',
RedisCluster::class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class RedisIntegrationTest extends AbstractCacheItemPoolIntegrationTest

protected function setUp(): void
{
/** @psalm-suppress MixedArrayAssignment */
$this->skippedTests['testHasItemReturnsFalseWhenDeferredItemIsExpired']
= 'Cache decorator does not support deferred deletion';

Expand Down
1 change: 0 additions & 1 deletion test/unit/RedisClusterOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public function testOptionConstantsMatchingExtensionImplementation(string $const
));
}

/** @psalm-suppress MixedAssignment */
$constantValueInOptions = constant($constantInOptions);
self::assertIsInt($constantValueInOptions);
self::assertEquals(
Expand Down

0 comments on commit c27a362

Please sign in to comment.