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

StaticGraphicParserTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testGetGraphic() 0 4 1
1
<?php
2
3
namespace Graze\CiffRenderer\Test;
4
5
use Graze\CiffRenderer\Field\Parser\StaticGraphicParser;
6
7
class StaticGraphicParserTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var \Graze\CiffRenderer\Field\Parser\ParserInterface
11
     */
12
    protected $parser;
13
14
    public function setUp()
15
    {
16
        $pathToFixture = sprintf('%s/Fixture/StaticGraphicParserFixture.xml', dirname(__FILE__));
17
18
        $xml = simplexml_load_file($pathToFixture);
19
20
        $this->parser = new StaticGraphicParser();
21
22
        $this->parser->setXmlField($xml);
23
    }
24
25
    public function testGetGraphic()
26
    {
27
        $this->assertEquals('w:/example.bmp', $this->parser->getGraphic());
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 getGraphic() does only exist in the following implementations of said interface: Graze\CiffRenderer\Field...ser\StaticGraphicParser.

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...
28
    }
29
}
30