Completed
Push — master ( 966347...8b759a )
by Gilles
08:12
created

Finder::find()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 8
nop 1
crap 8
1
<?php
2
3
namespace PHPHtmlParser;
4
5
use PHPHtmlParser\Dom\AbstractNode;
6
7
class Finder
8
{
9
    private $id;
10
11
    /**
12
     * Finder constructor.
13
     * @param $id
14
     */
15 9
    public function __construct($id)
16
    {
17 9
        $this->id = $id;
18 9
    }
19
20
    /**
21
     *
22
     * Find node in tree by id
23
     *
24
     * @param AbstractNode $node
25
     * @return bool|AbstractNode
26
     */
27 9
    public function find(AbstractNode $node)
28
    {
29 9
        if (!$node->id()) {
30 9
            return $this->find($node->firstChild());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PHPHtmlParser\Dom\AbstractNode as the method firstChild() does only exist in the following sub-classes of PHPHtmlParser\Dom\AbstractNode: PHPHtmlParser\Dom\HtmlNode, PHPHtmlParser\Dom\InnerNode, PHPHtmlParser\Dom\MockNode. 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...
31
        }
32
33 9
        if ($node->id() == $this->id) {
34 3
            return $node;
35
        }
36
37 9
        if ($node->hasNextSibling()) {
38 9
            $nextSibling = $node->nextSibling();
39 9
            if ($nextSibling->id() == $this->id) {
40 3
                return $nextSibling;
41
            }
42 6
            if ($nextSibling->id() > $this->id) {
43 3
                return $this->find($node->firstChild());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PHPHtmlParser\Dom\AbstractNode as the method firstChild() does only exist in the following sub-classes of PHPHtmlParser\Dom\AbstractNode: PHPHtmlParser\Dom\HtmlNode, PHPHtmlParser\Dom\InnerNode, PHPHtmlParser\Dom\MockNode. 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...
44
            }
45 6
            if ($nextSibling->id() < $this->id) {
46 6
                return $this->find($nextSibling);
47
            }
48 9
        } else if (!$node->isTextNode()) {
49 9
            return $this->find($node->firstChild());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PHPHtmlParser\Dom\AbstractNode as the method firstChild() does only exist in the following sub-classes of PHPHtmlParser\Dom\AbstractNode: PHPHtmlParser\Dom\HtmlNode, PHPHtmlParser\Dom\InnerNode, PHPHtmlParser\Dom\MockNode. 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...
50
        }
51
52 3
        return false;
53
    }
54
}