Passed
Push — master ( 373a1b...497e2b )
by Alain
02:37
created

ViewFinder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 127
Duplicated Lines 33.07 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 16 4
A initializeViews() 0 8 2
A initializeView() 21 21 4
A instantiateView() 21 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\View;
13
14
use BrightNucleus\View\Engine\EngineInterface;
15
use BrightNucleus\View\Exception\FailedToInstantiateViewException;
16
use BrightNucleus\View\Support\AbstractFinder;
17
18
/**
19
 * Class ViewFinder.
20
 *
21
 * @since   0.1.0
22
 *
23
 * @package BrightNucleus\View\View
24
 * @author  Alain Schlesser <[email protected]>
25
 */
26
class ViewFinder extends AbstractFinder
27
{
28
29
    /**
30
     * Find a result based on a specific criteria.
31
     *
32
     * @since 0.1.0
33
     *
34
     * @param array           $criteria Criteria to search for.
35
     * @param EngineInterface $engine   Optional. Engine to use with the view.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $engine not be null|EngineInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
36
     *
37
     * @return ViewInterface View that was found.
1 ignored issue
show
Documentation introduced by
Should the return type not be \BrightNucleus\View\Supp...View\Support\NullObject?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
38
     */
39 17
    public function find(array $criteria, EngineInterface $engine = null)
40
    {
41 17
        $uri = $criteria[0];
42
43 17
        $this->initializeViews($uri, $engine);
44
45 17
        foreach ($criteria as $entry) {
46 17
            foreach ($this->findables as $viewObject) {
47 17
                if ($viewObject->canHandle($entry)) {
48 17
                    return $viewObject;
49
                }
50
            }
51
        }
52
53
        return $this->nullObject;
54
    }
55
56
    /**
57
     * Initialize the views that can be iterated.
58
     *
59
     * @since 0.1.0
60
     *
61
     * @param string          $uri    URI to use for the view.
62
     * @param EngineInterface $engine Optional. Engine to use with the view.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $engine not be null|EngineInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
63
     */
64 17
    protected function initializeViews($uri, EngineInterface $engine = null)
65
    {
66 17
        foreach ($this->findables as &$view) {
67 17
            $view = $this->initializeView($view, $uri, $engine);
68
        }
69
70 17
        $this->nullObject = $this->initializeView($this->nullObject, $uri);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->initializeView($this->nullObject, $uri) of type object<BrightNucleus\View\View\ViewInterface> is incompatible with the declared type object<BrightNucleus\View\Support\NullObject> of property $nullObject.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71 17
    }
72
73
    /**
74
     * Initialize a single view by instantiating class name strings and calling closures.
75
     *
76
     * @since 0.1.0
77
     *
78
     * @param mixed           $view   View to instantiate.
79
     * @param string          $uri    URI to use for the view.
80
     * @param EngineInterface $engine Optional. Engine to use with the view.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $engine not be null|EngineInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
81
     *
82
     * @return ViewInterface Instantiated view.
83
     * @throws FailedToInstantiateViewException If the view could not be instantiated.
84
     */
85 17 View Code Duplication
    protected function initializeView($view, $uri, EngineInterface $engine = null)
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...
86
    {
87 17
        if (is_string($view)) {
88 7
            $view = new $view($uri, $engine);
89
        }
90
91 17
        if (is_callable($view)) {
92
            $view = $view($uri, $engine);
93
        }
94
95 17
        if (! $view instanceof ViewInterface) {
96
            throw new FailedToInstantiateViewException(
97
                sprintf(
98
                    _('Could not instantiate view "%s".'),
99
                    serialize($view)
100
                )
101
            );
102
        }
103
104 17
        return $view;
105
    }
106
107
    /**
108
     * Instantiate a view by instantiating class name strings and calling closures.
109
     *
110
     * @since 0.1.0
111
     *
112
     * @param mixed           $view   View to instantiate.
113
     * @param string          $uri    URI to use for the view.
114
     * @param EngineInterface $engine Optional. View to use with the view.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $engine not be null|EngineInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
115
     *
116
     * @return ViewInterface Instantiated view.
117
     * @throws FailedToInstantiateViewException If the view could not be instantiated.
118
     */
119 View Code Duplication
    protected function instantiateView($view, $uri, EngineInterface $engine = null)
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...
120
    {
121
        if (is_string($view)) {
122
            $view = new $view($uri, $engine);
123
        }
124
125
        if (is_callable($view)) {
126
            $view = $view($uri, $engine);
127
        }
128
129
        if (! $view instanceof ViewInterface) {
130
            throw new FailedToInstantiateViewException(
131
                sprintf(
132
                    _('Could not instantiate view "%s".'),
133
                    serialize($view)
134
                )
135
            );
136
        }
137
138
        return $view;
139
    }
140
141
    /**
142
     * Get the config key for the Findables definitions.
143
     *
144
     * @since 0.1.0
145
     *
146
     * @return string Config key use to define the Findables.
147
     */
148 7
    protected function getFindablesConfigKey()
149
    {
150 7
        return 'Views';
151
    }
152
}
153