Completed
Push — master ( cb2c73...1a2628 )
by Aleh
12s
created

ObjectCompleter   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 75
Duplicated Lines 25.33 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 0
cbo 11
dl 19
loc 75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D getEntries() 11 36 10
A canHandle() 0 4 3
A createEntryForMethod() 0 8 1
A createEntryForProperty() 8 8 2

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
namespace Padawan\Domain\Completer;
4
5
use Padawan\Domain\Core\Project;
6
use Padawan\Domain\Core\FQCN;
7
use Padawan\Domain\Core\Node\MethodData;
8
use Padawan\Domain\Core\Node\ClassProperty;
9
use Padawan\Domain\Core\Node\InterfaceData;
10
use Padawan\Domain\Core\Completion\Context;
11
use Padawan\Domain\Core\Completion\Entry;
12
use Padawan\Domain\Core\Collection\Specification;
13
use Psr\Log\LoggerInterface;
14
15
class ObjectCompleter extends AbstractInCodeBodyCompleter
16
{
17
    public function __construct(LoggerInterface $logger)
18
    {
19
        $this->logger = $logger;
20
    }
21
    public function getEntries(Project $project, Context $context)
22
    {
23
        /** @var FQCN $fqcn */
24
        list($fqcn, $isThis) = $context->getData();
25
        $this->logger->debug('creating entries');
26
        if (!$fqcn instanceof FQCN) {
27
            return [];
28
        }
29
        $index = $project->getIndex();
30
        $this->logger->debug('Creating completion for ' . $fqcn->toString());
31
        $class = $index->findClassByFQCN($fqcn);
32
        if (empty($class)) {
33
            $class = $index->findInterfaceByFQCN($fqcn);
34
        }
35
        if (empty($class)) {
36
            return [];
37
        }
38
        $entries = [];
39
        $spec = new Specification($isThis ? 'private' : 'public');
40 View Code Duplication
        if ($class->methods !== null) {
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...
41
            foreach ($class->methods->all($spec) as $method) {
42
                $entry = $this->createEntryForMethod($method);
43
                $entries[$method->name] = $entry;
44
            }
45
        }
46
        if ($class instanceof InterfaceData) {
47
            return $entries;
48
        }
49 View Code Duplication
        if ($class->properties !== null) {
0 ignored issues
show
Documentation introduced by
The property $properties is declared private in Padawan\Domain\Core\Node\ClassData. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

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...
50
            foreach ($class->properties->all($spec) as $property) {
0 ignored issues
show
Documentation introduced by
The property $properties is declared private in Padawan\Domain\Core\Node\ClassData. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
                $entries[$property->name] = $this->createEntryForProperty($property);
52
            }
53
        }
54
        ksort($entries);
55
        return $entries;
56
    }
57
58
    public function canHandle(Project $project, Context $context)
59
    {
60
        return parent::canHandle($project, $context) && ($context->isThis() || $context->isObject());
61
    }
62
63
    /**
64
     * Creates menu entry for MethodData
65
     *
66
     * @param MethodData $method a method
67
     * @return Entry
68
     */
69
    protected function createEntryForMethod(MethodData $method)
70
    {
71
        return new Entry(
72
            $method->name,
73
            $method->getSignature(),
74
            sprintf("%s\n%s\n", $method->getSignature(), $method->doc)
75
        );
76
    }
77
78 View Code Duplication
    protected function createEntryForProperty(ClassProperty $prop)
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...
79
    {
80
        $type = $prop->type instanceof FQCN ? $prop->type->getClassName() : 'mixed';
81
        return new Entry(
82
            $prop->name,
83
            $type
84
        );
85
    }
86
87
    /** @property LoggerInterface */
88
    private $logger;
89
}
90