Completed
Push — master ( 835840...ce2316 )
by Pablo
02:59
created

GitIgnoreExtractorHandlerTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A itShouldReturnNotNull() 0 20 1
1
<?php
2
3
namespace PhpGitHooks\Module\Git\Tests\Behaviour;
4
5
use PhpGitHooks\Module\Git\Contract\Query\GitIgnoreExtractor;
6
use PhpGitHooks\Module\Git\Contract\Query\GitIgnoreExtractorHandler;
7
use PhpGitHooks\Module\Git\Contract\Response\GitIgnoreDataResponse;
8
use PhpGitHooks\Module\Git\Tests\Infrastructure\GitUnitTestCase;
9
10
class GitIgnoreExtractorHandlerTest extends GitUnitTestCase
11
{
12
    /**
13
     * @var GitIgnoreExtractorHandler
14
     */
15
    private $gitIgnoreExtractorQueryHandler;
16
17
    protected function setUp()
18
    {
19
        $this->gitIgnoreExtractorQueryHandler = new GitIgnoreExtractorHandler($this->getGitIgnoreFileReader());
0 ignored issues
show
Bug introduced by
It seems like $this->getGitIgnoreFileReader() targeting PhpGitHooks\Module\Git\T...etGitIgnoreFileReader() can also be of type object<Mockery\MockInterface>; however, PhpGitHooks\Module\Git\C...rHandler::__construct() does only seem to accept object<PhpGitHooks\Modul...\Model\ReaderInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
20
    }
21
22
    /**
23
     * @test
24
     */
25
    public function itShouldReturnNotNull()
26
    {
27
        $content = 'composer.phar
28
        vendor/
29
        bin/
30
        php-git-hooks.yml
31
        # Commit your application\'s lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
32
        # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
33
        # composer.lock
34
        ';
35
36
        $this->shouldReadGitIgnoreFile($content);
37
38
        $query = new GitIgnoreExtractor();
39
40
        $data = $this->gitIgnoreExtractorQueryHandler->handle($query);
41
42
        $this->assertInstanceOf(GitIgnoreDataResponse::class, $data);
43
        $this->assertNotNull($data->getContent());
0 ignored issues
show
Bug introduced by
The method getContent does only exist in PhpGitHooks\Module\Git\C...e\GitIgnoreDataResponse, but not in PhpGitHooks\Module\Git\C...uery\GitIgnoreExtractor.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
44
    }
45
}
46