EntityClosure   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 158
Duplicated Lines 5.7 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 3
dl 9
loc 158
ccs 0
cts 60
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setActions() 0 4 1
A reflectedFunc() 0 14 4
A append() 0 8 1
A underlineCall() 0 14 1
A parseDocs() 9 19 2
A getMethod() 0 12 2
A getAction() 0 4 1
A redirect() 0 6 1
A group() 0 13 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
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\Entity;
14
15
use Apix\Entity,
16
    Apix\Entity\EntityInterface,
17
    Apix\Reflection,
18
    Apix\Router;
19
20
/**
21
 * Represents a resource.
22
 *
23
 */
24
class EntityClosure extends Entity implements EntityInterface
25
{
26
27
    /**
28
     * Holds group info...
29
     */
30
    public $group = null;
31
32
    // protected $group = null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
    // protected $group = array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
    //         // 'title' => 'some group title...',
35
    //         // 'description' => 'some group description..',
36
    //         // 'bb' => 'ss'
37
    //     );
38
39
    private $reflection;
40
41
    /**
42
     * Sets and returns the reflection of a function.
43
     *
44
     * @param  string                    $name The REST name of function.
45
     * @return \ReflectionFunction|false
46
     */
47
    public function reflectedFunc($name)
48
    {
49
        if (isset($this->reflection[$name])) {
50
            return $this->reflection[$name];
51
        } elseif ( isset($this->actions[$name]['action'])
52
            && $this->actions[$name]['action'] instanceof \Closure
53
        ) {
54
            $this->reflection[$name] = new \ReflectionFunction($this->actions[$name]['action']);
55
56
            return $this->reflection[$name];
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function append(array $def)
66
    {
67
        parent::_append($def);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (_append() instead of append()). Are you sure this is correct? If so, you might want to change this to $this->_append().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
68
        // if (!isset($def['method'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
        //     throw new \RuntimeException('Closure not defining a method, somehting must be wrong!?');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
        // }
71
        $this->actions[$def['method']] = $def;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     * @codeCoverageIgnore
77
     */
78
    public function setActions(array $asso = null)
79
    {
80
        // obsolete!
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function underlineCall(Router $route)
87
    {
88
        $method = $this->getMethod($route);
89
90
        #try {
91
            $action = $this->getAction($route->getMethod());
92
        #} catch (\Exception $e) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
        #    throw new \RuntimeException("Resource entity not (yet) implemented.", 501);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
        #}
95
96
        $params = $this->getValidatedParams($method, $route->getMethod(), $route->getParams());
97
98
        return call_user_func_array($action, $params);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function parseDocs()
105
    {
106
        // group class doc
107
        // $docs = Reflection::parsePhpDoc( $this->group );
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
108
        $docs = $this->group;
109
110
        // doc for all methods
111 View Code Duplication
        foreach ($this->getActions() as $key => $func) {
0 ignored issues
show
Bug introduced by
The expression $this->getActions() of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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...
112
            $ref = $this->reflectedFunc($key);
113
114
            $_docs = Reflection::parsePhpDoc($ref); // <- TODO (required args)
0 ignored issues
show
Documentation introduced by
$ref is of type object<ReflectionFunction>|false, but the function expects a object<Reflection>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
            $_docs['method'] = $key;
116
            // $_docs['path'] = $key . ' '; //. $this->getAction($key); // tood here
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
118
            $docs['methods'][$key] = $_docs;
119
        }
120
121
        return $docs;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getMethod(Router $route)
128
    {
129
        $name = $route->getMethod();
130
        if (false === $method = $this->reflectedFunc($name)) {
131
            throw new \InvalidArgumentException(
132
                "Invalid resource's method ({$name}) specified.",
133
                405
134
            );
135
        }
136
137
        return $method;
138
    }
139
140
    private function getAction($method)
141
    {
142
      return $this->actions[$method]['action'];
143
    }
144
145
    /* --- CLOSURE only --- */
146
147
    /**
148
     * Adds a redirect.
149
     *
150
     * @param  string $location A  name
151
     * @param  array  $resource The resource definition array
0 ignored issues
show
Bug introduced by
There is no parameter named $resource. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
152
     * @return void
153
     */
154
    public function redirect($location)
155
    {
156
        $this->redirect = $location;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Group a resource entity.
163
     *
164
     * @param  string $name The group name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
165
     * @return void
166
     */
167
    public function group($title)
168
    {
169
        // var_dump('dd');exit;
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
170
        // public $group = null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
171
        // $this->group = ['title'=> 'd', 'documentation'=>'some'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
172
173
        // TODO retrive phpdoc coment strinfg here!
174
175
        // group test
176
        $this->group = array('title' => $title);
177
178
        return $this;
179
    }
180
181
}
182