Completed
Pull Request — master (#97)
by Sam
04:21
created

ParseUnknownTypesTest::testUnknownType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Tests\Parser;
15
16
use Badcow\DNS\Classes;
17
use Badcow\DNS\Parser\ParseException;
18
use Badcow\DNS\Parser\Parser;
19
use Badcow\DNS\Rdata\A;
20
use Badcow\DNS\Rdata\PolymorphicRdata;
21
use Badcow\DNS\Rdata\UnknownType;
22
use Badcow\DNS\ResourceRecord;
23
use PHPUnit\Framework\TestCase;
24
25
class ParseUnknownTypesTest extends TestCase
26
{
27
    /**
28
     * @throws ParseException
29
     */
30 View Code Duplication
    public function testUnknownType(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $text = 'dickens.example.com. CLASS45 1800 TYPE1859 \# 20 412054616c65206f662054776f20436974696573';
33
        $binData = hex2bin('412054616c65206f662054776f20436974696573');
34
        $zone = Parser::parse('example.com.', $text);
35
        $this->assertCount(1, $zone);
36
        /** @var ResourceRecord $rr */
37
        $rr = $zone[0];
38
39
        $this->assertEquals('dickens.example.com.', $rr->getName());
40
        $this->assertEquals('CLASS45', $rr->getClass());
41
        $this->assertEquals(45, $rr->getClassId());
42
        $this->assertEquals('TYPE1859', $rr->getType());
43
        $this->assertEquals(1800, $rr->getTtl());
44
        $this->assertInstanceOf(UnknownType::class, $rr->getRdata());
45
        $this->assertEquals($binData, $rr->getRdata()->getData());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Badcow\DNS\Rdata\RdataInterface as the method getData() does only exist in the following implementations of said interface: Badcow\DNS\Rdata\PolymorphicRdata, Badcow\DNS\Rdata\UnknownType.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
46
        $this->assertEquals(1859, $rr->getRdata()->getTypeCode());
47
    }
48
49
    /**
50
     * @throws ParseException
51
     */
52 View Code Duplication
    public function testPolymorphicType(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $text = 'dickens.example.com. IN 1800 RESERVED "A Tale of Two Cities"';
55
        $zone = Parser::parse('example.com.', $text);
56
        $this->assertCount(1, $zone);
57
        /** @var ResourceRecord $rr */
58
        $rr = $zone[0];
59
60
        $this->assertEquals('dickens.example.com.', $rr->getName());
61
        $this->assertEquals('IN', $rr->getClass());
62
        $this->assertEquals(1, $rr->getClassId());
63
        $this->assertEquals('RESERVED', $rr->getType());
64
        $this->assertEquals(0xffff, $rr->getRdata()->getTypeCode());
65
        $this->assertEquals(1800, $rr->getTtl());
66
        $this->assertInstanceOf(PolymorphicRdata::class, $rr->getRdata());
67
        $this->assertEquals('"A Tale of Two Cities"', $rr->getRdata()->getData());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Badcow\DNS\Rdata\RdataInterface as the method getData() does only exist in the following implementations of said interface: Badcow\DNS\Rdata\PolymorphicRdata, Badcow\DNS\Rdata\UnknownType.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
68
    }
69
70
    public function testSupportedTypeInUnknownFormatIsOutputtedAsCorrectType(): void
71
    {
72
        //  files.example.com. IN 3600 IN A 192.168.1.100
73
        $record = 'files.example.com.  IN  3600    TYPE1   \# 4 c0 a8 01 64';
74
        $zone = Parser::parse('example.com.', $record);
75
        $this->assertCount(1, $zone);
76
        /** @var ResourceRecord $rr */
77
        $rr = $zone[0];
78
79
        $this->assertEquals('files.example.com.', $rr->getName());
80
        $this->assertEquals(Classes::INTERNET, $rr->getClass());
81
        $this->assertEquals(3600, $rr->getTtl());
82
        $this->assertInstanceOf(A::class, $rr->getRdata());
83
        $this->assertEquals('192.168.1.100', $rr->getRdata()->getAddress());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Badcow\DNS\Rdata\RdataInterface as the method getAddress() does only exist in the following implementations of said interface: Badcow\DNS\Rdata\A, Badcow\DNS\Rdata\AAAA, Badcow\DNS\Tests\Rdata\a...tests/Rdata/ATest.php$0, Badcow\DNS\Tests\Rdata\a...ts/Rdata/AaaaTest.php$0.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
84
    }
85
}
86