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

ParseZoneWithCommentsTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseZoneWithComments() 0 24 1
A testCommentOnlyLinesParse() 0 12 1
A testMultilineTxtRecords() 0 16 1
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\Parser\Comments;
17
use Badcow\DNS\Parser\Parser;
18
use Badcow\DNS\Rdata\A;
19
use Badcow\DNS\Rdata\AAAA;
20
use Badcow\DNS\Rdata\NS;
21
use Badcow\DNS\Rdata\SOA;
22
use Badcow\DNS\Rdata\TXT;
23
use PHPUnit\Framework\TestCase;
24
25
class ParseZoneWithCommentsTest extends TestCase
26
{
27
    /**
28
     * @throws \Exception
29
     */
30
    public function testParseZoneWithComments(): void
31
    {
32
        $zoneFile = NormaliserTest::readFile(__DIR__.'/Resources/testCollapseMultilines_sample.txt');
33
        $zone = Parser::parse('example.com.', $zoneFile, Comments::ALL);
34
35
        $nsRecords = ParserTest::findRecord('@', $zone, NS::TYPE);
36
        $this->assertCount(2, $nsRecords);
0 ignored issues
show
Documentation introduced by
$nsRecords 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...
37
38
        $mailTxtRecords = ParserTest::findRecord('mail', $zone, TXT::TYPE);
39
        $this->assertCount(1, $mailTxtRecords);
0 ignored issues
show
Documentation introduced by
$mailTxtRecords 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...
40
        $this->assertNull($mailTxtRecords[0]->getComment());
41
42
        $sub_domainRecords = ParserTest::findRecord('sub.domain', $zone, A::TYPE);
43
        $this->assertCount(1, $sub_domainRecords);
0 ignored issues
show
Documentation introduced by
$sub_domainRecords 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...
44
        $this->assertEquals('This is a local ip.', $sub_domainRecords[0]->getComment());
45
46
        $ipv6_domainRecords = ParserTest::findRecord('ipv6.domain', $zone, AAAA::TYPE);
47
        $this->assertCount(1, $ipv6_domainRecords);
0 ignored issues
show
Documentation introduced by
$ipv6_domainRecords 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...
48
        $this->assertEquals('This is an IPv6 domain.', $ipv6_domainRecords[0]->getComment());
49
50
        $soaRecords = ParserTest::findRecord('@', $zone, SOA::TYPE);
51
        $this->assertCount(1, $soaRecords);
0 ignored issues
show
Documentation introduced by
$soaRecords 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...
52
        $this->assertEquals('MNAME RNAME SERIAL REFRESH RETRY EXPIRE MINIMUM This is my Start of Authority Record; AKA SOA.', $soaRecords[0]->getComment());
53
    }
54
55
    /**
56
     * @throws \Exception
57
     */
58
    public function testCommentOnlyLinesParse(): void
59
    {
60
        $zoneFile = NormaliserTest::readFile(__DIR__.'/Resources/testCollapseMultilines_sample.txt');
61
        $zone = Parser::parse('example.com.', $zoneFile, Comments::ALL);
62
63
        $nullEntries = ParserTest::findRecord(null, $zone, null);
64
        $this->assertCount(4, $nullEntries);
0 ignored issues
show
Documentation introduced by
$nullEntries 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...
65
        $this->assertEquals('NS RECORDS', $nullEntries[0]->getComment());
66
        $this->assertEquals('A RECORDS', $nullEntries[1]->getComment());
67
        $this->assertEquals('AAAA RECORDS', $nullEntries[2]->getComment());
68
        $this->assertEquals('MX RECORDS', $nullEntries[3]->getComment());
69
    }
70
71
    /**
72
     * @throws \Exception
73
     */
74
    public function testMultilineTxtRecords(): void
75
    {
76
        $zoneFile = NormaliserTest::readFile(__DIR__.'/Resources/testMultilineTxtRecords_sample.txt');
77
        $zone = Parser::parse('acme.com.', $zoneFile, Comments::ALL);
78
79
        $txtRecords = ParserTest::findRecord('test', $zone, TXT::TYPE);
80
81
        $this->assertCount(1, $txtRecords);
0 ignored issues
show
Documentation introduced by
$txtRecords 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...
82
83
        $test = $txtRecords[0];
84
        $this->assertEquals('test', $test->getName());
85
        $this->assertEquals(7230, $test->getTtl());
86
        $this->assertEquals('TXT', $test->getType());
87
        $this->assertEquals('This is a comment.', $test->getComment());
88
        $this->assertEquals('This is an example of a multiline TXT record.', $test->getRdata()->getText());
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 getText() does only exist in the following implementations of said interface: Badcow\DNS\Rdata\SPF, Badcow\DNS\Rdata\TXT.

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...
89
    }
90
}
91