Completed
Push — master ( 296743...12eab9 )
by Kirill
36:17
created

ObjectBuilder::onCompile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 8
cts 8
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Reflection\Builder\Definitions;
11
12
use Railt\Parser\Ast\NodeInterface;
13
use Railt\Parser\Ast\RuleInterface;
14
use Railt\SDL\Base\Definitions\BaseObject;
15
use Railt\SDL\Reflection\Builder\Dependent\Field\FieldsBuilder;
16
use Railt\SDL\Reflection\Builder\DocumentBuilder;
17
use Railt\SDL\Reflection\Builder\Invocations\Directive\DirectivesBuilder;
18
use Railt\SDL\Reflection\Builder\Process\Compilable;
19
use Railt\SDL\Reflection\Builder\Process\Compiler;
20
21
/**
22
 * Class ObjectBuilder
23
 */
24 View Code Duplication
class ObjectBuilder extends BaseObject implements Compilable
0 ignored issues
show
Duplication introduced by
This class 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...
25
{
26
    use Compiler;
27
    use FieldsBuilder;
28
    use DirectivesBuilder;
29
30
    /**
31
     * SchemaBuilder constructor.
32
     * @param NodeInterface $ast
33
     * @param DocumentBuilder $document
34
     * @throws \Railt\SDL\Exceptions\TypeConflictException
35
     */
36 3318
    public function __construct(NodeInterface $ast, DocumentBuilder $document)
37
    {
38 3318
        $this->boot($ast, $document);
39 3318
        $this->offset = $this->offsetPrefixedBy('type');
40 3318
    }
41
42
    /**
43
     * @param NodeInterface|RuleInterface $ast
44
     * @return bool
45
     * @throws \Railt\SDL\Exceptions\TypeConflictException
46
     */
47 3317
    protected function onCompile(NodeInterface $ast): bool
48
    {
49 3317
        if ($ast->is('Implements')) {
50 127
            foreach ($ast->getChildren() as $child) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\NodeInterface as the method getChildren() does only exist in the following implementations of said interface: Railt\Compiler\Grammar\Delegate\IncludeDelegate, Railt\Compiler\Grammar\Delegate\RuleDelegate, Railt\Compiler\Grammar\Delegate\TokenDelegate, Railt\Parser\Ast\Rule.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
51 127
                $name = $child->getChild(0)->getValue();
52
53 127
                $interface = $this->load($name);
54
55 127
                $this->interfaces = $this->unique($this->interfaces, $interface);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->unique($this->interfaces, $interface) can also be of type object<Railt\SDL\Contrac...nitions\TypeDefinition>. However, the property $interfaces is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
56
            }
57
58 127
            return true;
59
        }
60
61 3317
        return false;
62
    }
63
}
64