Completed
Push — master ( b12741...03b4a1 )
by Kirill
01:38
created

GenericReducer::match()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of Properties package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Serafim\Properties\Reducers\TypeHint;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Serafim\Properties\Attribute\GenericTypeHint;
14
use Serafim\Properties\Attribute\Matchable;
15
use Serafim\Properties\Attribute\TypeHint;
16
use Serafim\Properties\Reducers\ReducerInterface;
17
18
/**
19
 * Class GenericReducer
20
 */
21
class GenericReducer implements ReducerInterface
22
{
23
    /**
24
     * @param RuleInterface $rule
25
     * @return bool
26
     */
27
    public function match(RuleInterface $rule): bool
28
    {
29
        return $rule->getName() === 'Generic';
30
    }
31
32
    /**
33
     * @param RuleInterface $rule
34
     * @return \Generator|Matchable
35
     */
36
    public function reduce(RuleInterface $rule): \Generator
37
    {
38
        /** @var string $name */
39
        $name = yield $rule->first('Type');
40
41
        $keyValue = [];
42
43
        foreach ($rule->first('GenericArguments') as $argument) {
0 ignored issues
show
Bug introduced by
The expression $rule->first('GenericArguments') of type null|object<Railt\Parser\Ast\NodeInterface> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
44
            $keyValue[] = yield $argument;
45
        }
46
47
        return new GenericTypeHint($name, ...$keyValue);
0 ignored issues
show
Documentation introduced by
$keyValue is of type array, but the function expects a null|object<Serafim\Prop...es\Attribute\Matchable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
    }
49
}
50