Completed
Push — master ( e42624...c35825 )
by Harry
9s
created

LocalCsvFileTest::testSettingOptionsModifiesTheDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 16
nc 1
nop 0
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
        static::assertNotSame($file, $clone);
32
33
        $clone->getFormat()->setDelimiter('--');
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 setDelimiter() 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...
34
35
        static::assertNotEquals($file->getFormat()->getDelimiter(), $clone->getFormat()->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...
36
    }
37
38
    public function testImplementsInterface()
39
    {
40
        $file = (new LocalFile('fake/path'))
41
            ->setFormat(new CsvFormat());
42
43
        static::assertInstanceOf(FormatAwareInterface::class, $file);
44
        static::assertInstanceOf(FormatInterface::class, $file->getFormat());
45
        static::assertInstanceOf(CsvFormatInterface::class, $file->getFormat());
46
    }
47
48
    public function testFormatTypeIsCsv()
49
    {
50
        $file = (new LocalFile('fake/path'))
51
            ->setFormat(new CsvFormat());
52
53
        static::assertEquals('csv', $file->getFormatType());
54
    }
55
}
56