ProcessFactoryTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 0 6 1
A testMake() 0 16 1
A testMakeException() 0 6 1
1
<?php
2
/**
3
 * File ProcessFactoryTest.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\ProcessQueue\Tests\Process;
8
9
use Epfremme\ProcessQueue\Process\ProcessFactory;
10
use Symfony\Component\Process\Process;
11
12
/**
13
 * Class ProcessFactoryTest
14
 *
15
 * @package Epfremme\ProcessQueue\Tests\Process
16
 */
17
class ProcessFactoryTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testConstruct()
20
    {
21
        $factory = new ProcessFactory('pwd');
22
23
        $this->assertAttributeEquals('pwd', 'cmd', $factory);
24
    }
25
26
    public function testMake()
27
    {
28
        $factory = new ProcessFactory('pwd');
29
30
        /** @var \SplFileInfo|\Mockery\MockInterface $fileinfo */
31
        $fileinfo = \Mockery::mock(\SplFileInfo::class);
32
        $fileinfo->shouldReceive('__toString')->andReturn('/tmp');
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in SplFileInfo.

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...
33
34
        $process = $factory->make($fileinfo);
0 ignored issues
show
Bug introduced by
It seems like $fileinfo defined by \Mockery::mock(\SplFileInfo::class) on line 31 can also be of type object<Mockery\MockInterface>; however, Epfremme\ProcessQueue\Pr...\ProcessFactory::make() does only seem to accept object<SplFileInfo>|string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35
36
        $this->assertInstanceOf(Process::class, $process);
37
        $this->assertEquals('pwd', $process->getCommandLine());
38
        $this->assertSame($fileinfo, $process->getWorkingDirectory());
39
        $this->assertFalse($process->isStarted());
40
        $this->assertFalse($process->isRunning());
41
    }
42
43
    /** @expectedException \Epfremme\ProcessQueue\Process\Exception\InvalidWorkingDirectoryException */
44
    public function testMakeException()
45
    {
46
        $factory = new ProcessFactory('pwd');
47
48
        $factory->make('/invalid/directory');
49
    }
50
}
51