Completed
Branch 0.8-dev (af4386)
by Kacper
03:08
created

LanguageToken::process()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5.0023

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 21
cts 22
cp 0.9545
rs 8.439
cc 5
eloc 19
nc 6
nop 4
crap 5.0023
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Parser\Token;
17
18
use Kadet\Highlighter\Language\Language;
19
use Kadet\Highlighter\Parser\Result;
20
use Kadet\Highlighter\Parser\TokenIterator;
21
22
/**
23
 * Class LanguageToken
24
 *
25
 * @package Kadet\Highlighter\Parser\Token
26
 *
27
 * @property bool $postProcess True if language is post processed.
28
 */
29
class LanguageToken extends Token
30
{
31 2
    public function getInjected()
32
    {
33 2
        return $this->rule->inject;
34
    }
35
36 11
    public function getLanguage()
37
    {
38 11
        return $this->getStart() ? $this->getStart()->rule->inject : $this->rule->language;
39
    }
40
41 11
    protected function validate(Language $language, $context)
42
    {
43 11
        $valid = false;
44
45 11
        if ($this->isStart()) {
46 2
            $lang = $this->rule->language;
47 2
            if ($lang === null && $this->getInjected() !== $language) {
48 1
                $valid = true;
49 2
            } elseif ($language === $lang && $this->rule->validator->validate($context)) {
50 1
                $valid = true;
51 1
            }
52 2
        } else {
53 11
            $desired = $this->getLanguage();
54 11
            $valid   = $language === $desired && $this->rule->validator->validate($context);
55
        }
56 11
        $this->setValid($valid);
57 11
    }
58
59 11
    public function process(array &$context, Language $language, Result $result, TokenIterator $tokens)
60
    {
61 11
        if(!$this->isValid($language, $context)) {
62
            return true;
63
        }
64
65 11
        if($this->isStart()) {
66 2
            $result->merge($this->getInjected()->parse($tokens));
67 2
        } else {
68 11
            $this->setStart($result[0]);
69
70 11
            if ($this->_start->postProcess) {
71 1
                $source = substr($tokens->getSource(), $this->_start->pos, $this->_start->getLength());
72 1
                $tokens = $this->_start->getInjected()->tokenize(
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Kadet\Highlighter\Parser\Token\Token as the method getInjected() does only exist in the following sub-classes of Kadet\Highlighter\Parser\Token\Token: Kadet\Highlighter\Parser\Token\LanguageToken. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
73 1
                    $source, $result, $this->_start->pos, Language::EMBEDDED_BY_PARENT
74 1
                );
75 1
                $result->exchangeArray($this->_start->getInjected()->parse($tokens)->getTokens());
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Kadet\Highlighter\Parser\Token\Token as the method getInjected() does only exist in the following sub-classes of Kadet\Highlighter\Parser\Token\Token: Kadet\Highlighter\Parser\Token\LanguageToken. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
76 1
            }
77
78
            # closing unclosed tokens
79 11
            foreach (array_reverse($context) as $hash => $name) {
80 1
                $end = new Token([$name, 'pos' => $this->pos]);
81 1
                $tokens[$hash]->setEnd($end);
82 1
                $result->append($end);
83 11
            }
84
85 11
            $result->append($this);
86 11
            return false;
87
        }
88
89 2
        return true;
90
    }
91
}
92