Completed
Pull Request — master (#8)
by
unknown
08:47
created

AttributesProcessor::getNext()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.2
c 1
b 0
f 0
cc 4
eloc 3
nc 5
nop 1
crap 20
1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-attributes-extension package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\CommonMark\AttributesExtension;
14
15
use League\CommonMark\Block\Element\AbstractBlock;
16
use League\CommonMark\Block\Element\Document;
17
use League\CommonMark\Block\Element\ListBlock;
18
use League\CommonMark\Block\Element\ListItem;
19
use League\CommonMark\DocumentProcessorInterface;
20
use League\CommonMark\Node\Node;
21
22
class AttributesProcessor implements DocumentProcessorInterface
23
{
24
    const DIRECTION_PREFIX = 'prefix';
25
    const DIRECTION_SUFFIX = 'suffix';
26
27
    public function processDocument(Document $document)
28
    {
29
        $walker = $document->walker();
30
        while ($event = $walker->next()) {
31
            $node = $event->getNode();
32
33
            if ($event->isEntering() || !$node instanceof Attributes) {
34
                continue;
35
            }
36
37
            list($target, $direction) = $this->findTargetAndDirection($node);
38
39
            if ($target) {
40 View Code Duplication
                if (($parent = $target->parent()) instanceof ListItem && $parent->parent() instanceof ListBlock && $parent->parent()->isTight()) {
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
                    $target = $parent;
42
                }
43
44
                if ($direction === self::DIRECTION_SUFFIX) {
45
                    $attributes = AttributesUtils::merge($target, $node->getAttributes());
46
                } else {
47
                    $attributes = AttributesUtils::merge($node->getAttributes(), $target);
48
                }
49
50
                $target->data['attributes'] = $attributes;
0 ignored issues
show
Bug introduced by
The property data does not seem to exist in League\CommonMark\Node\Node.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
51
            }
52
53
            if ($node instanceof AbstractBlock && $node->endsWithBlankLine() && $node->next() && $node->previous()) {
54
                $node->previous()->setLastLineBlank(true);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class League\CommonMark\Node\Node as the method setLastLineBlank() does only exist in the following sub-classes of League\CommonMark\Node\Node: League\CommonMark\Block\Element\AbstractBlock, League\CommonMark\Block\Element\BlockQuote, League\CommonMark\Block\Element\Document, League\CommonMark\Block\Element\FencedCode, League\CommonMark\Block\Element\Heading, League\CommonMark\Block\Element\HtmlBlock, League\CommonMark\Block\Element\IndentedCode, League\CommonMark\Block\Element\ListBlock, League\CommonMark\Block\Element\ListItem, League\CommonMark\Block\Element\Paragraph, League\CommonMark\Block\Element\ThematicBreak, Webuni\CommonMark\AttributesExtension\Attributes, Webuni\CommonMark\TableExtension\Table, Webuni\CommonMark\TableExtension\TableCaption, Webuni\CommonMark\TableExtension\TableCell, Webuni\CommonMark\TableExtension\TableRow, Webuni\CommonMark\TableExtension\TableRows. 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...
55
            }
56
57
            $node->detach();
58
        }
59
    }
60
61
    private function findTargetAndDirection(Node $node)
62
    {
63
        $target = null;
64
        $direction = null;
65
        $previous = $next = $node;
66
        while (true) {
67
            $previous = $this->getPrevious($previous);
68
            $next = $this->getNext($next);
69
70
            if ($previous === null && $next === null) {
71
                $target = $node->parent();
72
                $direction = self::DIRECTION_SUFFIX;
73
                break;
74
            }
75
76 View Code Duplication
            if ($previous !== null && !$previous instanceof Attributes) {
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...
77
                $target = $previous;
78
                $direction = self::DIRECTION_SUFFIX;
79
                break;
80
            }
81
82 View Code Duplication
            if ($next !== null && !$next instanceof Attributes) {
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...
83
                $target = $next;
84
                $direction = self::DIRECTION_PREFIX;
85
                break;
86
            }
87
        }
88
89
        return [$target, $direction];
90
    }
91
92
    private function getPrevious(Node $node = null)
93
    {
94
        $previous = $node instanceof Node ? $node->previous() : null;
95
96
        if ($previous instanceof AbstractBlock && $previous->endsWithBlankLine()) {
97
            $previous = null;
98
        }
99
100
        return $previous;
101
    }
102
103
    private function getNext(Node $node = null)
104
    {
105
        if ($node instanceof Node) {
106
            return $node instanceof AbstractBlock && $node->endsWithBlankLine() ? null : $node->next();
107
        }
108
    }
109
}
110