WorkflowNormalizer::supportsDenormalization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) 2019, Wesley O. Nichols
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Wesnick\WorkflowBundle\Serializer;
13
14
use ApiPlatform\Core\JsonLd\Serializer\ItemNormalizer;
15
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
16
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
17
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18
use Symfony\Component\Serializer\SerializerAwareInterface;
19
use Symfony\Component\Serializer\SerializerInterface;
20
use Wesnick\WorkflowBundle\Model\PotentialActionInterface;
21
use Wesnick\WorkflowBundle\WorkflowActionGenerator;
22
23
/**
24
 * Class WorkflowNormalizer.
25
 *
26
 * @author Wesley O. Nichols <[email protected]>
27
 */
28
class WorkflowNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface, ContextAwareDenormalizerInterface, SerializerAwareInterface
29
{
30
    /**
31
     * @var ItemNormalizer
32
     */
33
    private $decorated;
34
    private $customNormalizer;
35
    private $workflowActions;
36
37
    public function __construct(
38
        NormalizerInterface $decorated,
39
        NormalizerInterface $customNormalizer,
40
        WorkflowActionGenerator $workflowActions
41
    ) {
42
        $this->decorated = $decorated;
0 ignored issues
show
Documentation Bug introduced by
$decorated is of type Symfony\Component\Serial...zer\NormalizerInterface, but the property $decorated was declared to be of type ApiPlatform\Core\JsonLd\Serializer\ItemNormalizer. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
43
        $this->customNormalizer = $customNormalizer;
44
        $this->workflowActions = $workflowActions;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function supportsNormalization($data, $format = null): bool
51
    {
52
        return $this->decorated->supportsNormalization($data, $format);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function hasCacheableSupportsMethod(): bool
59
    {
60
        return true;
61
    }
62
63
    public function normalize($object, $format = null, array $context = [])
64
    {
65
        if ($object instanceof PotentialActionInterface) {
66
            $actions = $this->workflowActions->getActionsForSubject($object);
67
            foreach ($actions as $action) {
68
                $object->addPotentialAction($action);
69
            }
70
        }
71
72
        return $this->decorated->normalize($object, $format, $context);
73
    }
74
75
    public function supportsDenormalization($data, $type, $format = null, array $context = [])
76
    {
77
        return $this->decorated->supportsDenormalization($data, $type, $format, $context);
0 ignored issues
show
Unused Code introduced by
The call to ApiPlatform\Core\JsonLd\...pportsDenormalization() has too many arguments starting with $context. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        return $this->decorated->/** @scrutinizer ignore-call */ supportsDenormalization($data, $type, $format, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
78
    }
79
80
    public function denormalize($data, $class, $format = null, array $context = [])
81
    {
82
        return $this->decorated->denormalize($data, $class, $format, $context);
83
    }
84
85
    public function setSerializer(SerializerInterface $serializer)
86
    {
87
        $this->decorated->setSerializer($serializer);
88
    }
89
}
90