Completed
Push — master ( 4ff2ad...4ba911 )
by Franck
03:40
created

EntityClass   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 7.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 10
loc 136
ccs 0
cts 74
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A reflectedClass() 0 14 3
A append() 0 8 2
B underlineCall() 0 25 1
A parseDocs() 10 23 3
A getMethod() 0 12 2
A setActions() 0 10 2
A getMethods() 0 6 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 class based entity resource.
22
 */
23
class EntityClass extends Entity implements EntityInterface
24
{
25
    protected $controller;
26
27
    private $reflection;
28
29
    /**
30
     * Sets and returns a reflection of the entity class.
31
     *
32
     * @return \ReflectionClass|false
33
     */
34
    public function reflectedClass()
35
    {
36
        if (null === $this->reflection) {
37
            try {
38
                $this->reflection = new \ReflectionClass(
39
                    $this->controller['name']
40
                );
41
            } catch (\Exception $Exception) {
42
                throw new \RuntimeException('Resource entity class not (yet) implemented.', 501);
43
            }
44
        }
45
46
        return $this->reflection;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function append(array $def)
53
    {
54
        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...
55
56
        if (isset($def['controller'])) {
57
            $this->controller = $def['controller'];
58
        }
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function underlineCall(Router $route)
65
    {
66
        $name = $this->controller['name'];
67
        $args = $this->controller['args'];
68
69
        // if (!isset($entity->controller['name'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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
        //     $entity->controller['name'] = $route->controller_name;
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
71
        // }
72
73
        // just created a deependency here!!!!!
74
        // if (!isset($args)) {
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...
75
        //     $args = $route->controller_args;
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...
76
        // }
77
78
        $method = $this->getMethod($route);
79
80
        $params = $this->getValidatedParams($method, $route->getMethod(), $route->getParams());
81
82
        return call_user_func_array(
83
          array(
84
            new $name($args),
85
            $route->getAction()),
86
            $params
87
          );
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function parseDocs()
94
    {
95
        // group class doc
96
        $docs = Reflection::parsePhpDoc(
97
            $this->reflectedClass()->getDocComment()
98
        );
99
100
        $actions = $this->getActions();
101
102
        // doc for all methods
103 View Code Duplication
        foreach ($this->getMethods() as $key => $method) {
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...
104
            if ( $key = array_search($method->name, $actions) ) {
105
106
               $_docs = Reflection::parsePhpDoc( $method );
107
                $_docs['method'] = $key;
108
109
                // temp
110
                $docs['methods'][$key] = $_docs;
111
            }
112
        }
113
114
        return $docs;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getMethod(Router $route)
121
    {
122
        $name = $route->getAction();
123
        if (false === $this->reflectedClass()->hasMethod($name)) {
124
            throw new \InvalidArgumentException(
125
                "Invalid resource's method ({$route->getMethod()}) specified.",
126
                405
127
            );
128
        }
129
130
        return $this->reflectedClass()->getMethod($name);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function setActions(array $asso = null)
137
    {
138
        $funcs = array();
139
        foreach ($this->getMethods() as $ref) {
140
            $funcs[] = $ref->name;
141
        }
142
        $routes = Router::$actions;
143
144
        $this->actions = array_intersect($routes, $funcs);
145
    }
146
147
    /**
148
     * Returns the class methods. Use internalaly.
149
     * @return array An array of \ReflectionMethod objects.
150
     */
151
    public function getMethods()
152
    {
153
        return $this->reflectedClass()->getMethods(
154
            \ReflectionMethod::IS_STATIC | \ReflectionMethod::IS_PUBLIC
155
        );
156
    }
157
158
}
159