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

CustomHandlerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A spfHandler() 0 14 2
A testCustomHandler() 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\Classes;
17
use Badcow\DNS\Parser\ParseException;
18
use Badcow\DNS\Parser\Parser;
19
use Badcow\DNS\Rdata\TXT;
20
use PHPUnit\Framework\TestCase;
21
22
class CustomHandlerTest extends TestCase
23
{
24
    public function spfHandler(\ArrayIterator $iterator): TXT
25
    {
26
        $string = '';
27
        while ($iterator->valid()) {
28
            $string .= $iterator->current().' ';
29
            $iterator->next();
30
        }
31
        $string = trim($string, ' "'); //Remove whitespace and quotes
32
33
        $spf = new TXT();
34
        $spf->setText($string);
35
36
        return $spf;
37
    }
38
39
    /**
40
     * @throws ParseException
41
     */
42
    public function testCustomHandler(): void
43
    {
44
        $customHandlers = ['SPF' => [$this, 'spfHandler']];
45
46
        $record = 'example.com. 7200 IN SPF "v=spf1 a mx ip4:69.64.153.131 include:_spf.google.com ~all"';
47
        $parser = new Parser($customHandlers);
48
        $zone = $parser->makeZone('example.com.', $record);
49
        $rr = $zone->getResourceRecords()[0];
50
51
        $this->assertEquals('TXT', $rr->getType());
52
        $this->assertEquals('example.com.', $rr->getName());
53
        $this->assertEquals(7200, $rr->getTtl());
54
        $this->assertEquals(Classes::INTERNET, $rr->getClass());
55
        $this->assertNotNull($rr->getRdata());
56
        $this->assertEquals('v=spf1 a mx ip4:69.64.153.131 include:_spf.google.com ~all', $rr->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...
57
    }
58
}
59