LocalCsvFileTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testCloneWillCloneTheCsvDefinition() 0 16 1
A testImplementsInterface() 0 9 1
A testFormatTypeIsCsv() 0 7 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Unit\Node;
15
16
use Graze\DataFile\Format\CsvFormat;
17
use Graze\DataFile\Format\CsvFormatInterface;
18
use Graze\DataFile\Format\FormatAwareInterface;
19
use Graze\DataFile\Format\FormatInterface;
20
use Graze\DataFile\Node\LocalFile;
21
use Graze\DataFile\Test\TestCase;
22
23
class LocalCsvFileTest extends TestCase
24
{
25
    public function testCloneWillCloneTheCsvDefinition()
26
    {
27
        $file = (new LocalFile('some/path/here'))
28
            ->setFormat(new CsvFormat());
29
        $clone = $file->getClone();
30
31
        $format = $clone->getFormat();
32
        static::assertInstanceOf(CsvFormatInterface::class, $format);
33
        static::assertInstanceOf(CsvFormat::class, $format);
34
35
        static::assertNotSame($file, $clone);
36
37
        $format->setDelimiter('--');
38
39
        static::assertNotEquals($file->getFormat()->getDelimiter(), $format->getDelimiter());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Graze\DataFile\Format\FormatInterface as the method getDelimiter() does only exist in the following implementations of said interface: Graze\DataFile\Format\CsvFormat.

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...
40
    }
41
42
    public function testImplementsInterface()
43
    {
44
        $file = (new LocalFile('fake/path'))
45
            ->setFormat(new CsvFormat());
46
47
        static::assertInstanceOf(FormatAwareInterface::class, $file);
48
        static::assertInstanceOf(FormatInterface::class, $file->getFormat());
49
        static::assertInstanceOf(CsvFormatInterface::class, $file->getFormat());
50
    }
51
52
    public function testFormatTypeIsCsv()
53
    {
54
        $file = (new LocalFile('fake/path'))
55
            ->setFormat(new CsvFormat());
56
57
        static::assertEquals('csv', $file->getFormatType());
58
    }
59
}
60