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

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

Labels
Severity

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
20
class Paragraph extends AbstractStringContainerBlock implements InlineContainerInterface
21
{
22
    /**
23
     * Returns true if this block can contain the given block as a child node
24
     *
25
     * @param AbstractBlock $block
26
     *
27
     * @return bool
28
     */
29 69
    public function canContain(AbstractBlock $block): bool
30
    {
31 69
        return false;
32
    }
33
34
    /**
35
     * Whether this is a code block
36
     *
37
     * @return bool
38
     */
39 381
    public function isCode(): bool
40
    {
41 381
        return false;
42
    }
43
44 723
    public function matchesNextLine(Cursor $cursor): bool
45
    {
46 723
        if ($cursor->isBlank()) {
47 441
            $this->lastLineBlank = true;
48
49 441
            return false;
50
        }
51
52 381
        return true;
53
    }
54
55 1659
    public function finalize(ContextInterface $context, int $endLineNumber)
56
    {
57 1659
        parent::finalize($context, $endLineNumber);
58
59 1659
        $this->finalStringContents = \preg_replace('/^  */m', '', \implode("\n", $this->getStrings()));
60
61
        // Short-circuit
62 1659
        if ($this->finalStringContents === '' || $this->finalStringContents[0] !== '[') {
63 1311
            return;
64
        }
65
66 426
        $cursor = new Cursor($this->finalStringContents);
67
68 426
        $referenceFound = $this->parseReferences($context, $cursor);
69
70 426
        $this->finalStringContents = $cursor->getRemainder();
71
72 426
        if ($referenceFound && $cursor->isAtEnd()) {
73 222
            $this->detach();
74
        }
75 426
    }
76
77
    /**
78
     * @param ContextInterface $context
79
     * @param Cursor           $cursor
80
     *
81
     * @return bool
82
     */
83 426
    protected function parseReferences(ContextInterface $context, Cursor $cursor)
84
    {
85 426
        $referenceFound = false;
86 426
        while ($cursor->getCharacter() === '[' && $context->getReferenceParser()->parse($cursor)) {
87 231
            $this->finalStringContents = $cursor->getRemainder();
88 231
            $referenceFound = true;
89
        }
90
91 426
        return $referenceFound;
92
    }
93
94
    /**
95
     * @param ContextInterface $context
96
     * @param Cursor           $cursor
97
     */
98 276
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
99
    {
100 276
        $cursor->advanceToNextNonSpaceOrTab();
101 276
        $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...
102 276
    }
103
104
    /**
105
     * @return string[]
106
     */
107 1698
    public function getStrings(): array
108
    {
109 1698
        return $this->strings->toArray();
110
    }
111
}
112