Completed
Pull Request — master (#190)
by
unknown
10:49
created

Context::getGuesserManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Knp\FriendlyContexts\Context;
4
5
use Behat\Behat\Context\Context as ContextInterface;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Knp\FriendlyContexts\Dictionary\Backgroundable;
8
use Knp\FriendlyContexts\Dictionary\Taggable;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
11
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
12
13
abstract class Context implements ContextInterface
14
{
15
    use Backgroundable,
16
        Taggable;
17
18
    protected $config = [];
19
    protected $container;
20
21
    public function initialize(array $config, ContainerInterface $container)
22
    {
23
        $this->config    = array_merge($this->getDefaultOptions(), $config);
24
        $this->container = $container;
25
    }
26
27
    protected function getRecordBag()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
28
    {
29
        return $this->get('friendly.record.bag');
30
    }
31
32
    protected function getEntityHydrator()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
33
    {
34
        return $this->get('friendly.entity.hydrator');
35
    }
36
37
    protected function getEntityResolver()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
38
    {
39
        return $this->get('friendly.entity.resolver');
40
    }
41
42
    protected function getTextFormater()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
43
    {
44
        return $this->get('friendly.text.formater');
45
    }
46
47
    protected function getAsserter()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
48
    {
49
        return $this->get('friendly.asserter');
50
    }
51
52
    protected function getGuesserManager()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
53
    {
54
        return $this->get('friendly.guesser.manager');
55
    }
56
57
    protected function getObjectReflector()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
58
    {
59
        return $this->get('friendly.object.reflector');
60
    }
61
62
    protected function getFeatureWalker()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
63
    {
64
        return $this->get('friendly.feature.walker');
65
    }
66
67
    protected function getAliceLoader()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
68
    {
69
        return $this->get('friendly.alice.fixtures.loader');
70
    }
71
72
    /**
73
     * Gets Doctrine manager
74
     *
75
     * @return ObjectManager
76
     */
77
    protected function getManager()
78
    {
79
        $doctrineService = isset($this->config['alice']['doctrine_service']) ? $this->config['alice']['doctrine_service'] : 'doctrine';
80
81
        return $this->get($doctrineService)->getManager();
82
    }
83
84
    protected function getUniqueCache()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
85
    {
86
        return $this->get('friendly.unique_cache');
87
    }
88
89
    protected function getPageClassResolver()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
90
    {
91
        return $this->get('friendly.page.resolver');
92
    }
93
94
    protected function getRequestBuilder()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
95
    {
96
        return $this->get('friendly.builder.request_builder');
97
    }
98
99
    protected function getHttpContentTypeGuesser()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
100
    {
101
        return $this->get('friendly.http.http_content_type_guesser');
102
    }
103
104 View Code Duplication
    protected function get($service)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
105
    {
106
        if ($this->container->has($service)) {
107
            return $this->container->get($service);
108
        }
109
110
        if (null !== $this->getKernel() && $this->getKernel()->getContainer()->has($service)) {
111
            return $this->getKernel()->getContainer()->get($service);
112
        }
113
114
        throw new ServiceNotFoundException($service);
115
    }
116
117 View Code Duplication
    protected function getParameter($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
118
    {
119
        if ($this->container->hasParameter($name)) {
120
            return $this->container->getParameter($name);
121
        }
122
123
        if (null !== $this->getKernel() && $this->getKernel()->getContainer()->hasParameter($name)) {
124
            return $this->getKernel()->getContainer()->getParameter($name);
125
        }
126
127
        throw new ParameterNotFoundException($name);
128
    }
129
130
    protected function getKernel()
131
    {
132
        if ($this->container->has('friendly.symfony.kernel')) {
133
            $kernel = $this->container->get('friendly.symfony.kernel');
134
            $kernel->boot();
135
136
            return $kernel;
137
        }
138
    }
139
140
    protected function resolveEntity($name)
141
    {
142
        $namespaces = $this->getParameter('friendly.entities.namespaces');
143
144
        $entities = $this
145
            ->getEntityResolver()
146
            ->resolve(
147
                $this->getManager(),
148
                $name,
149
                empty($namespaces) ? '' : $namespaces
150
            )
151
        ;
152
153
        switch (true) {
154
            case 1 < count($entities):
155
                throw new \Exception(
156
                    sprintf(
157
                        'Failed to find a unique model from the name "%s", "%s" found',
158
                        $name,
159
                        implode('" and "', array_map(
160
                            function ($rfl) {
161
                                return $rfl->getName();
162
                            },
163
                            $entities
164
                        ))
165
                    )
166
                );
167
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
168
            case 0 === count($entities):
169
                throw new \Exception(
170
                    sprintf(
171
                        'Failed to find a model from the name "%s"',
172
                        $name
173
                    )
174
                );
175
        }
176
177
        return current($entities);
178
    }
179
180
    protected function getDefaultOptions()
181
    {
182
        return [];
183
    }
184
}
185