Completed
Pull Request — master (#90)
by
unknown
14:30
created

Finder::find()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 28
rs 6.7272
cc 7
eloc 16
nc 7
nop 1
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
    public function __construct($id)
16
    {
17
        $this->id = $id;
18
    }
19
20
    /**
21
     *
22
     * Find node in tree
23
     *
24
     * @param AbstractNode $node
25
     * @return bool|AbstractNode
26
     */
27
    public function find(AbstractNode $node)
28
    {
29
30
        if (!$node->id()) {
31
            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...
32
        }
33
34
        if ($node->id() == $this->id) {
35
            return $node;
36
        }
37
38
        if ($node->hasNextSibling()) {
39
            $nextSibling = $node->nextSibling();
40
            if ($nextSibling->id() == $this->id) {
41
                return $nextSibling;
42
            }
43
            if ($nextSibling->id() > $this->id) {
44
                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...
45
            }
46
            if ($nextSibling->id() < $this->id) {
47
                return $this->find($nextSibling);
48
            }
49
        } else {
50
            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...
51
        }
52
53
        return false;
54
    }
55
56
}