Skip to content

Commit

Permalink
fixed issues reported by psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
arusinowski committed Sep 13, 2023
1 parent 9e606a4 commit 0447fec
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 139 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"cakephp/cakephp-codesniffer": "^5.0",
"cakephp/bake": "^3.0.0",
"cakephp/migrations": "^4.0.0",
"aura/intl": "^3.0"
"aura/intl": "^3.0",
"vimeo/psalm": "^5.15"
},
"autoload": {
"psr-4": {
Expand Down
119 changes: 5 additions & 114 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,126 +1,17 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
allowCoercionFromStringToClassConst="true"
allowStringToStandInForClass="true"
errorLevel="3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />

<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->

<MissingClosureReturnType errorLevel="info" />
<MissingPropertyType errorLevel="info" />

<PropertyNotSetInConstructor errorLevel="info" />
<MissingConstructor>
</MissingConstructor>
<MissingClosureParamType errorLevel="info" />
<MissingParamType errorLevel="info" />

<RedundantCondition>
<errorLevel type="suppress">
</errorLevel>
</RedundantCondition>

<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />

<UnresolvableInclude errorLevel="info" />

<!-- level 4 issues - points to possible deficiencies in logic, higher false-positives -->

<MoreSpecificReturnType errorLevel="info" />
<LessSpecificReturnStatement errorLevel="info" />
<TypeCoercion errorLevel="info" />

<PossiblyInvalidArrayAccess errorLevel="info" />
<PossiblyInvalidArrayOffset errorLevel="info" />
<PossiblyInvalidFunctionCall errorLevel="info" />
<PossiblyInvalidIterator errorLevel="info" />
<PossiblyInvalidMethodCall errorLevel="info" />
<PossiblyInvalidOperand errorLevel="info" />
<PossiblyInvalidPropertyAssignment errorLevel="info" />
<PossiblyNullArgument errorLevel="info" />
<PossiblyNullArrayAccess errorLevel="info" />
<PossiblyNullArrayAssignment errorLevel="info" />
<PossiblyNullArrayOffset errorLevel="info" />
<PossiblyNullOperand errorLevel="info" />
<PossiblyNullPropertyAssignment errorLevel="info" />
<PossiblyNullPropertyAssignmentValue errorLevel="info" />
<PossiblyNullPropertyFetch errorLevel="info" />
<PossiblyNullReference errorLevel="info" />

<!-- level 5 issues - should be avoided at mosts costs... -->

<InvalidScalarArgument errorLevel="info" />
<InvalidOperand errorLevel="info" />
<NoInterfaceProperties errorLevel="info" />
<TypeDoesNotContainType errorLevel="info" />
<TypeDoesNotContainNull errorLevel="info" />
<ImplementedReturnTypeMismatch errorLevel="info" />

<!-- level 6 issues - really bad things -->

<NullableReturnStatement>
<errorLevel type="suppress">
</errorLevel>
</NullableReturnStatement>

<MoreSpecificImplementedParamType errorLevel="info" />
<LessSpecificImplementedReturnType errorLevel="info" />

<!-- level 7 issues - even worse -->
<InvalidArgument errorLevel="info" />

<InvalidPropertyAssignmentValue>
<errorLevel type="suppress">
</errorLevel>
</InvalidPropertyAssignmentValue>

<!-- CakePHP Specific -->
<DeprecatedClass>
<errorLevel type="suppress">
</errorLevel>
</DeprecatedClass>

<PossiblyUndefinedArrayOffset>
</PossiblyUndefinedArrayOffset>

<UndefinedConstant errorLevel="suppress" />

<UndefinedPropertyAssignment>
</UndefinedPropertyAssignment>

<UndefinedPropertyFetch>
</UndefinedPropertyFetch>

<EmptyArrayAccess>
</EmptyArrayAccess>

<LoopInvalidation>
</LoopInvalidation>

<UndefinedClass>
</UndefinedClass>

<UndefinedMethod>
</UndefinedMethod>

<NullReference>
</NullReference>

<PossiblyUndefinedMethod>
</PossiblyUndefinedMethod>

</issueHandlers>
</psalm>
14 changes: 8 additions & 6 deletions src/Model/Behavior/EnumBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use CakeDC\Enum\Model\Behavior\Strategy\ConstStrategy;
use CakeDC\Enum\Model\Behavior\Strategy\LookupStrategy;
use CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface;
use function Cake\I18n\__d;

class EnumBehavior extends Behavior
{
Expand Down Expand Up @@ -63,7 +64,7 @@ class EnumBehavior extends Behavior
* ];
* ```
*
* @var array
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'defaultStrategy' => 'lookup',
Expand Down Expand Up @@ -135,7 +136,10 @@ public function strategy(string $alias, mixed $strategy): StrategyInterface
throw new MissingEnumStrategyException([$class]);
}

return $this->strategies[$alias] = new $class($alias, $this->_table);
/** @var \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface $strategy */
$strategy = new $class($alias, $this->_table);

return $this->strategies[$alias] = $strategy;
}

/**
Expand Down Expand Up @@ -219,7 +223,7 @@ protected function enumList(string $alias, array $config): array
if ($this->getConfig('nested')) {
array_walk(
$return,
function (&$item, $val): void {
function (mixed &$item, mixed $val): void {
$item = ['value' => $val, 'text' => $item];
}
);
Expand All @@ -240,9 +244,7 @@ protected function translate(array $list): array
{
$domain = $this->getConfig('translationDomain');

return array_map(function ($value) use ($domain) {
return __d($domain, $value);
}, $list);
return array_map(fn($value) => __d($domain, $value), $list);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Model/Behavior/Strategy/AbstractStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cake\Core\InstanceConfigTrait;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use function Cake\I18n\__d;

abstract class AbstractStrategy implements StrategyInterface
{
Expand Down
25 changes: 7 additions & 18 deletions src/Model/Behavior/Strategy/ConstStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ArrayObject;
use Cake\Collection\CollectionInterface;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Entity;
use Cake\ORM\Query\SelectQuery;
Expand Down Expand Up @@ -64,9 +65,7 @@ public function enum(array $config = []): array
}
}

$values = array_map(function ($v) use ($constants) {
return $constants[$v];
}, $keys);
$values = array_map(fn($v): mixed => $constants[$v], $keys);

return array_combine($keys, $values);
}
Expand All @@ -91,7 +90,7 @@ protected function getConstants(): array
$constants = [];

foreach ($classConstants as $key => $value) {
if (str_starts_with($key, (string)$prefix)) {
if (str_starts_with($key, $prefix)) {
$listKey = substr($key, $length);
if ($lowercase) {
$listKey = strtolower($listKey);
Expand Down Expand Up @@ -124,12 +123,8 @@ public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObjec

$query->clearContain()->contain($contain);

$query->formatResults(function (CollectionInterface $results) {
return $results->map(function ($row) {
if (is_string($row) || !$row) {
return $row;
}

$query->formatResults(fn(CollectionInterface $results) => $results
->map(function (EntityInterface $row): EntityInterface {
$constant = Hash::get($row, $this->getConfig('field'));

$field = Inflector::singularize(Inflector::underscore($this->alias));
Expand All @@ -139,17 +134,11 @@ public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObjec
'value' => $constant,
], ['markClean' => true, 'markNew' => false]);

if (is_array($row)) {
$row[$field] = $value->toArray();

return $row;
}

$row->set($field, $value);
$row->setDirty($field, false);

return $row;
});
});
})
);
}
}

0 comments on commit 0447fec

Please sign in to comment.