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

StaticCompleter::canHandle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 2
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 StaticCompleter 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 static 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', 1);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

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...
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
        if ($class->constants !== null) {
0 ignored issues
show
Documentation introduced by
The property $constants 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...
55
            foreach ($class->constants->all() as $const) {
0 ignored issues
show
Documentation introduced by
The property $constants 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...
56
                $entries[$const] = $this->createEntryForConst($const);
57
            }
58
        }
59
        ksort($entries);
60
        return $entries;
61
    }
62
63
    public function canHandle(Project $project, Context $context)
64
    {
65
        return parent::canHandle($project, $context) && $context->isClassStatic();
66
    }
67
68
    /**
69
     * Creates menu entry for MethodData
70
     *
71
     * @param MethodData $method a method
72
     * @return Entry
73
     */
74
    protected function createEntryForMethod(MethodData $method)
75
    {
76
        return new Entry(
77
            $method->name,
78
            $method->getSignature(),
79
            sprintf("%s\n%s\n", $method->getSignature(), $method->doc)
80
        );
81
    }
82
83 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...
84
    {
85
        $type = $prop->type instanceof FQCN ? $prop->type->toString() : 'mixed';
86
        return new Entry(
87
            '$' . $prop->name,
88
            $type
89
        );
90
    }
91
92
    protected function createEntryForConst($const)
93
    {
94
        return new Entry($const);
95
    }
96
97
    /** @property LoggerInterface */
98
    private $logger;
99
}
100