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

AbstractStringContainerBlock::getStringContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\ArrayCollection;
20
21
abstract class AbstractStringContainerBlock extends AbstractBlock implements StringContainerInterface
22
{
23
    /**
24
     * @var ArrayCollection|string[]
25
     */
26
    protected $strings;
27
28
    /**
29
     * @var string
30
     */
31
    protected $finalStringContents = '';
32
33
    /**
34
     * Constructor
35
     */
36 2040
    public function __construct()
37
    {
38 2040
        $this->strings = new ArrayCollection();
39 2040
    }
40
41
    /**
42
     * @param string $line
43
     */
44 2019
    public function addLine(string $line)
45
    {
46 2019
        $this->strings->add($line);
47 2019
    }
48
49
    /**
50
     * @param ContextInterface $context
51
     * @param Cursor           $cursor
52
     */
53
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
54
    {
55
        // TODO: Is this even really needed? Can/should this be abstract instead?
56
        // create paragraph container for line
57
        $context->addBlock(new Paragraph());
58
        $cursor->advanceToNextNonSpaceOrTab();
59
        $context->getTip()->addLine($cursor->getRemainder());
0 ignored issues
show
Bug introduced by Colin O'Dell
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...
60
    }
61
62 1992
    public function getStringContent(): string
63
    {
64 1992
        return $this->finalStringContents;
65
    }
66
}
67