Completed
Push — master ( fbf2a7...00045a )
by Colin
02:48
created

src/InlineParserContext.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;
16
17
use League\CommonMark\Block\Element\AbstractBlock;
18
use League\CommonMark\Delimiter\DelimiterStack;
19
use League\CommonMark\Reference\ReferenceMap;
20
21
class InlineParserContext
22
{
23
    private $container;
24
    private $referenceMap;
25
    private $cursor;
26
    private $delimiterStack;
27
28 1752
    public function __construct(AbstractBlock $container, ReferenceMap $referenceMap)
29
    {
30 1752
        $this->referenceMap = $referenceMap;
31 1752
        $this->container = $container;
32 1752
        $this->cursor = new Cursor(\trim($container->getStringContent()));
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 getStringContent() 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...
33 1752
        $this->delimiterStack = new DelimiterStack();
34 1752
    }
35
36
    /**
37
     * @return AbstractBlock
38
     */
39 1293
    public function getContainer(): AbstractBlock
40
    {
41 1293
        return $this->container;
42
    }
43
44
    /**
45
     * @return ReferenceMap
46
     */
47 429
    public function getReferenceMap(): ReferenceMap
48
    {
49 429
        return $this->referenceMap;
50
    }
51
52
    /**
53
     * @return Cursor
54
     */
55 1752
    public function getCursor(): Cursor
56
    {
57 1752
        return $this->cursor;
58
    }
59
60
    /**
61
     * @return DelimiterStack
62
     */
63 1743
    public function getDelimiterStack(): DelimiterStack
64
    {
65 1743
        return $this->delimiterStack;
66
    }
67
}
68