AbstractProcessor   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 10.96 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 56.52%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 8
loc 73
ccs 13
cts 23
cp 0.5652
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createLink() 0 17 2
A resolveGetter() 8 17 4
A resolveSetter() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Definition\ConfigurationProcessor;
5
6
use Mikemirten\Component\JsonApi\Mapper\Definition\Link;
7
8
/**
9
 * Abstract processor
10
 * Contains shared methods
11
 *
12
 * @package Mikemirten\Component\JsonApi\Mapper\Definition\ConfigurationProcessor
13
 */
14
abstract class AbstractProcessor implements ConfigurationProcessorInterface
15
{
16
    /**
17
     * Pattern of "resource" parameter of link annotation
18
     */
19
    const RESOURCE_PATTERN = '~^(?<repository>[a-z_][a-z0-9_]*)\.(?<link>[a-z_][a-z0-9_]*)$~i';
20
21
    /**
22
     * Create link
23
     *
24
     * @param  string $name
25
     * @param  array  $data
26
     * @return Link
27
     */
28 2
    public function createLink(string $name, array $data): Link
29
    {
30 2
        if (! preg_match(self::RESOURCE_PATTERN, $data['resource'], $matches)) {
31
            throw new \LogicException(sprintf('Invalid resource definition: "%s"', $data['resource']));
32
        }
33
34 2
        $link = new Link(
35 2
            $name,
36 2
            $matches['repository'],
37 2
            $matches['link']
38
        );
39
40 2
        $link->setParameters($data['parameters']);
41 2
        $link->setMetadata($data['metadata']);
42
43 2
        return $link;
44
    }
45
46
    /**
47
     * Resolve getter of related object
48
     *
49
     * @param  \ReflectionClass $reflection
50
     * @param  string           $name
51
     * @return string
52
     */
53
    protected function resolveGetter(\ReflectionClass $reflection, string $name)
54
    {
55 View Code Duplication
        foreach (['get', 'is'] as $prefix)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
        {
57
            $getter = $prefix . ucfirst($name);
58
59
            if ($reflection->hasMethod($getter) && $reflection->getMethod($getter)->isPublic()) {
60
                return $getter;
61
            }
62
        }
63
64
        throw new \LogicException(sprintf(
65
            'Getter-method for "%s" cannot be resolved automatically. ' .
66
            'Probably there is no get%2$s() or is%2$s() method or it is not public.',
67
            $name, ucfirst($name)
68
        ));
69
    }
70
71
    /**
72
     * Resolve getter of related object
73
     *
74
     * @param  \ReflectionClass $reflection
75
     * @param  string           $name
76
     * @return string | null
77
     */
78 16
    protected function resolveSetter(\ReflectionClass $reflection, string $name)
79
    {
80 16
        $setter = 'set' . ucfirst($name);
81
82 16
        if ($reflection->hasMethod($setter) && $reflection->getMethod($setter)->isPublic()) {
83
            return $setter;
84
        }
85
    }
86
}