Completed
Push — master ( db8578...85f9c3 )
by Kirill
10:30
created

PragmasResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 36
c 0
b 0
f 0
ccs 0
cts 18
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 16 4
A badPragma() 0 6 1
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\Compiler\Grammar\PP2\Resolvers;
11
12
use Railt\Compiler\Exception\UnknownPragmaException;
13
use Railt\Compiler\Grammar\PP2\Delegate\PragmaDefinitionDelegate;
14
use Railt\Compiler\Grammar\PP2\ResolverInterface;
15
use Railt\Compiler\Reader\BasePragmas;
16
use Railt\Io\Exception\ExternalFileException;
17
use Railt\Io\Readable;
18
use Railt\Parser\Ast\RuleInterface;
19
20
/**
21
 * Class PragmasResolver
22
 */
23
class PragmasResolver extends BasePragmas implements ResolverInterface
24
{
25
    /**
26
     * @param Readable $readable
27
     * @param RuleInterface|PragmaDefinitionDelegate $rule
28
     * @throws \Railt\Io\Exception\ExternalFileException
29
     */
30
    public function resolve(Readable $readable, RuleInterface $rule): void
31
    {
32
        [$name, $value] = [$rule->getPragmaName(), $rule->getPragmaValue()];
0 ignored issues
show
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\RuleInterface as the method getPragmaName() does only exist in the following implementations of said interface: Railt\Compiler\Grammar\P...ragmaDefinitionDelegate.

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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\RuleInterface as the method getPragmaValue() does only exist in the following implementations of said interface: Railt\Compiler\Grammar\P...ragmaDefinitionDelegate.

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...
33
34
        foreach ($this->getResolvers() as $group => $resolver) {
35
            if ($resolver->match($name)) {
36
                $resolved = $resolver->resolve($name);
37
38
                if (! $resolved) {
39
                    throw $this->badPragma($name, $value)->throwsIn($readable, $rule->getOffset());
40
                }
41
42
                $this->set($group, $name, $value);
43
            }
44
        }
45
    }
46
47
    /**
48
     * @param string $name
49
     * @param string $value
50
     * @return ExternalFileException
51
     */
52
    private function badPragma(string $name, string $value): ExternalFileException
53
    {
54
        $error = \sprintf('Unknown pragma name "%s" with value "%s"', $name, $value);
55
56
        return new UnknownPragmaException($error);
57
    }
58
}
59