Completed
Pull Request — master (#15)
by John
02:41
created

OffsetDateParserTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 3
c 3
b 1
f 0
lcom 1
cbo 2
dl 0
loc 37
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 17 1
A testGetDateFormat() 0 4 1
A testGetText() 0 6 1
1
<?php
2
3
namespace Graze\CiffRenderer\Test;
4
5
use Graze\CiffRenderer\Field\Parser\DateParser\OffsetDateParser;
6
7
class OffsetDateParserTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var \Graze\CiffRenderer\Field\Parser\ParserInterface
11
     */
12
    protected $parser;
13
14
    public function setUp()
15
    {
16
        $pwd = dirname(__FILE__);
17
18
        $pathToFixture = sprintf('%s/Fixture/OffsetDateParserFixture.xml', $pwd);
19
20
        $xml = simplexml_load_file($pathToFixture);
21
22
        $pathToHeaderFixture = sprintf('%s/Fixture/OffsetDateParserHeaderFixture.xml', $pwd);
23
24
        $xmlHeader = simplexml_load_file($pathToHeaderFixture);
25
26
        $this->parser = new OffsetDateParser();
27
28
        $this->parser->setXmlField($xml);
29
        $this->parser->setXmlHeader($xmlHeader);
30
    }
31
32
    public function testGetDateFormat()
33
    {
34
        $this->assertSame("MM'/'dd'/'yy", $this->parser->getDateFormat());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\CiffRenderer\Field\Parser\ParserInterface as the method getDateFormat() does only exist in the following implementations of said interface: Graze\CiffRenderer\Field...rser\AbstractDateParser, Graze\CiffRenderer\Field...r\DateParser\DateParser, Graze\CiffRenderer\Field...Parser\OffsetDateParser.

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...
35
    }
36
37
    public function testGetText()
38
    {
39
        $expected = (new \DateTime())->modify('+10 days')->format('m/d/y');
40
41
        $this->assertEquals($expected, $this->parser->getText());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\CiffRenderer\Field\Parser\ParserInterface as the method getText() does only exist in the following implementations of said interface: Graze\CiffRenderer\Field\Parser\BarcodeParser, Graze\CiffRenderer\Field...rser\AbstractDateParser, Graze\CiffRenderer\Field...r\DateParser\DateParser, Graze\CiffRenderer\Field...Parser\OffsetDateParser, Graze\CiffRenderer\Field\Parser\FixedTextParser.

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...
42
    }
43
}
44