Completed
Push — master ( aeeeb2...74d969 )
by Colin
03:28
created

src/Block/Element/HtmlBlock.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Block\Element;
16
17
use League\CommonMark\ContextInterface;
18
use League\CommonMark\Cursor;
19
use League\CommonMark\Util\RegexHelper;
20
21
class HtmlBlock extends AbstractStringContainerBlock
22
{
23
    // Any changes to these constants should be reflected in .phpstorm.meta.php
24
    const TYPE_1_CODE_CONTAINER = 1;
25
    const TYPE_2_COMMENT = 2;
26
    const TYPE_3 = 3;
27
    const TYPE_4 = 4;
28
    const TYPE_5_CDATA = 5;
29
    const TYPE_6_BLOCK_ELEMENT = 6;
30
    const TYPE_7_MISC_ELEMENT = 7;
31
32
    /**
33
     * @var int
34
     */
35
    protected $type;
36
37
    /**
38
     * @param int $type
39
     */
40 177
    public function __construct(int $type)
41
    {
42 177
        parent::__construct();
43
44 177
        $this->type = $type;
45 177
    }
46
47
    /**
48
     * @return int
49
     */
50
    public function getType(): int
51
    {
52
        return $this->type;
53
    }
54
55
    /**
56
     * @param int $type
57
     */
58
    public function setType(int $type)
59
    {
60
        $this->type = $type;
61
    }
62
63
    /**
64
     * Returns true if this block can contain the given block as a child node
65
     *
66
     * @param AbstractBlock $block
67
     *
68
     * @return bool
69
     */
70
    public function canContain(AbstractBlock $block): bool
71
    {
72
        return false;
73
    }
74
75
    /**
76
     * Whether this is a code block
77
     *
78
     * @return bool
79
     */
80 111
    public function isCode(): bool
81
    {
82 111
        return true;
83
    }
84
85 132
    public function matchesNextLine(Cursor $cursor): bool
86
    {
87 132
        if ($cursor->isBlank() && ($this->type === self::TYPE_6_BLOCK_ELEMENT || $this->type === self::TYPE_7_MISC_ELEMENT)) {
88 51
            return false;
89
        }
90
91 111
        return true;
92
    }
93
94 162
    public function finalize(ContextInterface $context, int $endLineNumber)
95
    {
96 162
        parent::finalize($context, $endLineNumber);
97
98 162
        $this->finalStringContents = \implode("\n", $this->strings->toArray());
99 162
    }
100
101
    /**
102
     * @param ContextInterface $context
103
     * @param Cursor           $cursor
104
     */
105 162
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
106
    {
107 162
        $context->getTip()->addLine($cursor->getRemainder());
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class League\CommonMark\Block\Element\AbstractBlock as the method addLine() does only exist in the following sub-classes of League\CommonMark\Block\Element\AbstractBlock: League\CommonMark\Block\...actStringContainerBlock, League\CommonMark\Block\Element\FencedCode, League\CommonMark\Block\Element\Heading, League\CommonMark\Block\Element\HtmlBlock, League\CommonMark\Block\Element\IndentedCode, League\CommonMark\Block\Element\Paragraph. 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...
108
109
        // Check for end condition
110 162
        if ($this->type >= self::TYPE_1_CODE_CONTAINER && $this->type <= self::TYPE_5_CDATA) {
111 60
            if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->type)) !== null) {
112 57
                $this->finalize($context, $context->getLineNumber());
113
            }
114
        }
115 162
    }
116
}
117