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

ReverseRecordTest::testParseReverseRecordFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 9.1781
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\PTR;
20
use PHPUnit\Framework\TestCase;
21
22
class ReverseRecordTest extends TestCase
23
{
24
    /**
25
     * @throws ParseException
26
     */
27
    public function testReverseRecord(): void
28
    {
29
        $ptr = '1    1080 IN    PTR  gw01.core.acme.com.';
30
        $zone = Parser::parse('50.100.200.in-addr.arpa.', $ptr);
31
        $rr = $zone->getResourceRecords()[0];
32
33
        $this->assertEquals('1', $rr->getName());
34
        $this->assertEquals(Classes::INTERNET, $rr->getClass());
35
        $this->assertEquals(PTR::TYPE, $rr->getType());
36
        $this->assertEquals('gw01.core.acme.com.', $rr->getRdata()->getTarget());
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 getTarget() does only exist in the following implementations of said interface: Badcow\DNS\Rdata\CNAME, Badcow\DNS\Rdata\DNAME, Badcow\DNS\Rdata\NS, Badcow\DNS\Rdata\PTR, Badcow\DNS\Rdata\SRV, Badcow\DNS\Rdata\URI.

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...
37
    }
38
39
    /**
40
     * @throws ParseException|\Exception
41
     */
42
    public function testParseReverseRecordFile(): void
43
    {
44
        $file = NormaliserTest::readFile(__DIR__.'/Resources/50.100.200.in-addr.arpa.db');
45
        $zone = Parser::parse('50.100.200.in-addr.arpa.', $file);
46
47
        $parentRecords = ParserTest::findRecord('@', $zone);
48
        $_1Records = ParserTest::findRecord('1', $zone);
49
        $_50Records = ParserTest::findRecord('50', $zone);
50
        $_150Records = ParserTest::findRecord('150', $zone);
51
        $_170Records = ParserTest::findRecord('170', $zone);
52
53
        $this->assertCount(11, $zone);
54
        $this->assertCount(3, $parentRecords);
0 ignored issues
show
Documentation introduced by
$parentRecords is of type array<integer,object<Badcow\DNS\ResourceRecord>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        $this->assertCount(2, $_1Records);
0 ignored issues
show
Documentation introduced by
$_1Records is of type array<integer,object<Badcow\DNS\ResourceRecord>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
        $this->assertCount(1, $_50Records);
0 ignored issues
show
Documentation introduced by
$_50Records is of type array<integer,object<Badcow\DNS\ResourceRecord>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
        $this->assertCount(1, $_150Records);
0 ignored issues
show
Documentation introduced by
$_150Records is of type array<integer,object<Badcow\DNS\ResourceRecord>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
59
        $_1 = $_1Records[0];
60
        $_50 = $_50Records[0];
61
        $_150 = $_150Records[0];
62
        $_170 = $_170Records[0];
63
64
        $this->assertEquals('1', $_1->getName());
65
        $this->assertEquals(1080, $_1->getTtl());
66
        $this->assertEquals(Classes::INTERNET, $_1->getClass());
67
        $this->assertEquals(PTR::TYPE, $_1->getType());
68
        $this->assertEquals('gw01.core.acme.com.', $_1->getRdata()->getTarget());
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 getTarget() does only exist in the following implementations of said interface: Badcow\DNS\Rdata\CNAME, Badcow\DNS\Rdata\DNAME, Badcow\DNS\Rdata\NS, Badcow\DNS\Rdata\PTR, Badcow\DNS\Rdata\SRV, Badcow\DNS\Rdata\URI.

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...
69
70
        $this->assertEquals('50', $_50->getName());
71
        $this->assertEquals(1080, $_50->getTtl());
72
        $this->assertEquals(Classes::INTERNET, $_50->getClass());
73
        $this->assertEquals(PTR::TYPE, $_50->getType());
74
        $this->assertEquals('mx1.acme.com.', $_50->getRdata()->getTarget());
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 getTarget() does only exist in the following implementations of said interface: Badcow\DNS\Rdata\CNAME, Badcow\DNS\Rdata\DNAME, Badcow\DNS\Rdata\NS, Badcow\DNS\Rdata\PTR, Badcow\DNS\Rdata\SRV, Badcow\DNS\Rdata\URI.

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...
75
76
        $this->assertEquals('150', $_150->getName());
77
        $this->assertEquals(200, $_150->getTtl());
78
        $this->assertEquals(Classes::INTERNET, $_150->getClass());
79
        $this->assertEquals(PTR::TYPE, $_150->getType());
80
        $this->assertEquals('smtp.example.com.', $_150->getRdata()->getTarget());
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 getTarget() does only exist in the following implementations of said interface: Badcow\DNS\Rdata\CNAME, Badcow\DNS\Rdata\DNAME, Badcow\DNS\Rdata\NS, Badcow\DNS\Rdata\PTR, Badcow\DNS\Rdata\SRV, Badcow\DNS\Rdata\URI.

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...
81
82
        $this->assertEquals('170', $_170->getName());
83
        $this->assertEquals(150, $_170->getTtl());
84
        $this->assertEquals(Classes::INTERNET, $_170->getClass());
85
        $this->assertEquals(PTR::TYPE, $_170->getType());
86
        $this->assertEquals('netscape.com.', $_170->getRdata()->getTarget());
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 getTarget() does only exist in the following implementations of said interface: Badcow\DNS\Rdata\CNAME, Badcow\DNS\Rdata\DNAME, Badcow\DNS\Rdata\NS, Badcow\DNS\Rdata\PTR, Badcow\DNS\Rdata\SRV, Badcow\DNS\Rdata\URI.

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...
87
    }
88
}
89