AliceContext   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 107
Duplicated Lines 11.21 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 12
loc 107
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B loadAlice() 4 24 4
A resolveDepsFromArray() 0 10 2
A resolveDeps() 0 15 4
B loadFixtures() 0 16 5
A getPersistableClasses() 0 15 4
A registerCache() 8 16 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
3
namespace Knp\FriendlyContexts\Context;
4
5
class AliceContext extends Context
6
{
7
    /**
8
     * @BeforeBackground
9
     **/
10
    public function loadAlice($event)
11
    {
12
        $this->storeTags($event);
13
        $fixtures = $this->getParameter('friendly.alice.fixtures');
14
        $loader = $this->getAliceLoader();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
15
16
        $files = $this->getTagContent('alice');
17
18
        if (in_array('*', $files)) {
19
            $files = array_keys($fixtures);
20
        } else {
21
            $files = $this->resolveDepsFromArray($files);
22
        }
23
24
        foreach ($files as $name) {
25 View Code Duplication
            if (!array_key_exists($name, $fixtures)) {
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...
26
27
                throw new \Exception(sprintf('Fixture "%s" unknown. "%s" expected', $name, implode('", "', array_keys($fixtures))));
28
            }
29
        }
30
31
        $this->loadFixtures($loader, $fixtures, $files);
32
        $this->registerCache($loader);
33
    }
34
35
    protected function loadFixtures($loader, $fixtures, $files)
36
    {
37
        $persistable = $this->getPersistableClasses();
38
39
        foreach ($fixtures as $id => $fixture) {
40
            if (in_array($id, $files)) {
41
                foreach ($loader->load($fixture) as $object) {
42
                    if (in_array(get_class($object), $persistable)) {
43
                        $this->getEntityManager()->persist($object);
44
                    }
45
                }
46
47
                $this->getEntityManager()->flush();
48
            }
49
        }
50
    }
51
52
    private function getPersistableClasses()
53
    {
54
        $persistable = array();
55
        $metadatas   = $this->getEntityManager()->getMetadataFactory()->getAllMetadata();
56
57
        foreach ($metadatas as $metadata) {
58
            if (isset($metadata->isEmbeddedClass) && $metadata->isEmbeddedClass) {
59
                continue;
60
            }
61
62
            $persistable[] = $metadata->getName();
63
        }
64
65
        return $persistable;
66
    }
67
68
    protected function registerCache($loader)
69
    {
70
        foreach ($loader->getCache() as $cache) {
71
            list($values, $entity) = $cache;
72
            $reflection = new \ReflectionClass($entity);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73 View Code Duplication
            do {
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...
74
                $this
75
                    ->getRecordBag()
76
                    ->getCollection($reflection->getName())
77
                    ->attach($entity, $values)
78
                ;
79
                $reflection = $reflection->getParentClass();
80
            } while (false !== $reflection);
81
        }
82
        $loader->clearCache();
83
    }
84
85
    protected function resolveDepsFromArray(array $fixtures)
86
    {
87
        $result = [];
88
89
        foreach ($fixtures as $fixture) {
90
            $this->resolveDeps($fixture, $result);
91
        }
92
93
        return $result;
94
    }
95
96
    protected function resolveDeps($fixture, array &$result = [])
97
    {
98
        $result[] = $fixture;
99
        $tree = $this->getParameter('friendly.alice.dependencies');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
100
101
        if (!empty($tree[$fixture])) {
102
            foreach ($tree[$fixture] as $dep) {
103
                if (!in_array($dep, $result)) {
104
                    $this->resolveDeps($dep, $result);
105
                }
106
            }
107
        }
108
109
        return $result;
110
    }
111
}
112