Passed
Push — master ( 497e2b...a46ae4 )
by Alain
02:30
created

EngineFinder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 11.36 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 12
c 3
b 0
f 1
lcom 1
cbo 3
dl 10
loc 88
ccs 20
cts 24
cp 0.8333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 14 4
A initializeEngines() 10 10 3
A initializeEngine() 0 21 4
A getFindablesConfigKey() 0 4 1

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
 * Bright Nucleus View Component.
4
 *
5
 * @package   BrightNucleus\View
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\View\Engine;
13
14
use BrightNucleus\View\Exception\FailedToInstantiateEngineException;
15
use BrightNucleus\View\Support\AbstractFinder;
16
17
/**
18
 * Class EngineFinder.
19
 *
20
 * @since   0.1.0
21
 *
22
 * @package BrightNucleus\View\Engine
23
 * @author  Alain Schlesser <[email protected]>
24
 */
25
class EngineFinder extends AbstractFinder
26
{
27
28
    /**
29
     * Find a result based on a specific criteria.
30
     *
31
     * @since 0.1.0
32
     *
33
     * @param array $criteria Criteria to search for.
34
     *
35
     * @return mixed Result of the search.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \BrightNucleus\View\Supp...View\Support\NullObject.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
36
     */
37 17
    public function find(array $criteria)
38
    {
39 17
        $this->initializeEngines();
40
41 17
        foreach ($criteria as $entry) {
42 17
            foreach ($this->findables as $engine) {
43 17
                if ($engine->canHandle($entry)) {
44 17
                    return $engine;
45
                }
46
            }
47
        }
48
49
        return $this->nullObject;
50
    }
51
52
    /**
53
     * Initialize the engines that can be iterated.
54
     *
55
     * @since 0.1.0
56
     *
57
     */
58 17 View Code Duplication
    protected function initializeEngines()
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...
59
    {
60 17
        foreach ($this->findables as &$engine) {
61 17
            $engine = $this->initializeEngine($engine);
62
        }
63
64 17
        if (! is_object($this->nullObject)) {
65 7
            $this->nullObject = new $this->nullObject();
66
        }
67 17
    }
68
69
    /**
70
     * Initialize a single engine by instantiating class name strings and calling closures.
71
     *
72
     * @since 0.1.0
73
     *
74
     * @param mixed $engine Engine to instantiate.
75
     *
76
     * @return EngineInterface Instantiated engine.
77
     * @throws FailedToInstantiateEngineException If the engine could not be instantiated.
78
     */
79 17
    protected function initializeEngine($engine)
80
    {
81 17
        if (is_string($engine)) {
82 7
            $engine = new $engine();
83
        }
84
85 17
        if (is_callable($engine)) {
86
            $engine = $engine();
87
        }
88
89 17
        if (! $engine instanceof EngineInterface) {
90
            throw new FailedToInstantiateEngineException(
91
                sprintf(
92
                    _('Could not instantiate engine "%s".'),
93
                    serialize($engine)
94
                )
95
            );
96
        }
97
98 17
        return $engine;
99
    }
100
101
    /**
102
     * Get the config key for the Findables definitions.
103
     *
104
     * @since 0.1.0
105
     *
106
     * @return string Config key use to define the Findables.
107
     */
108 7
    protected function getFindablesConfigKey()
109
    {
110 7
        return 'Engines';
111
    }
112
}
113