Completed
Push — develop ( 4b49c4...89d32a )
by Jaap
09:06 queued 05:30
created

ParseCommandTest::testDefaultTargetDirCreation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 87
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 68
nc 1
nop 0
dl 0
loc 87
rs 8.6296
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 *  For the full copyright and license information, please view the LICENSE
6
 *  file that was distributed with this source code.
7
 *
8
 *  @copyright 2010-2017 Mike van Riel<[email protected]>
9
 *  @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 *  @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Parser\Command\Project;
14
15
use Mockery\Adapter\Phpunit\MockeryTestCase;
16
use phpDocumentor\Command\Helper\ConfigurationHelper;
17
use phpDocumentor\Command\Helper\LoggerHelper;
18
use phpDocumentor\Console\Output\Output;
19
use phpDocumentor\Descriptor\ProjectDescriptor;
20
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
21
use phpDocumentor\DomainModel\Parser\FileCollector;
22
use phpDocumentor\Fileset\Collection;
23
use phpDocumentor\Parser\Command\Project\ParseCommand;
24
use \Mockery as m;
25
use phpDocumentor\Parser\Parser;
26
use phpDocumentor\Reflection\DocBlock\ExampleFinder;
27
use Symfony\Component\Console\Helper\HelperSet;
28
use Symfony\Component\Console\Input\ArrayInput;
29
use Zend\Cache\Storage\StorageInterface;
30
use Zend\I18n\Translator\Translator;
31
32
/**
33
 * @coversDefaultClass phpDocumentor\Parser\Command\Project\ParseCommand
34
 * @covers ::<protected>
35
 */
36
class ParseCommandTest extends MockeryTestCase
37
{
38
    /**
39
     * Tests the processing of target directory when non is provided.
40
     * @covers ::execute
41
     */
42
    public function testDefaultTargetDirCreation()
43
    {
44
        $input = new ArrayInput([]);
45
        $output = new Output();
46
47
        $configurationHelper = m::mock(ConfigurationHelper::class);
48
        $configurationHelper->shouldReceive('getName')
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
49
            ->andReturn('phpdocumentor_configuration');
50
        $configurationHelper->shouldReceive('setHelperSet');
51
        $configurationHelper->shouldReceive('getOption')
52
            ->with($input, 'extensions', 'parser/extensions', array('php', 'php3', 'phtml'), true)
53
            ->andReturn([]);
54
        $configurationHelper->shouldReceive('getOption')
55
            ->with($input, 'target', 'parser/target');
56
        $configurationHelper->shouldReceive('getOption')
57
            ->with($input, m::any(), m::any(), m::any(), m::any())
58
            ->andReturn([]);
59
        $configurationHelper->shouldReceive('getOption')
60
            ->with($input, m::any(), m::any(), m::any())
61
            ->andReturn([]);
62
        $configurationHelper->shouldReceive('getOption')
63
            ->with($input, m::any(), m::any())
64
            ->andReturn('Title');
65
        $configurationHelper->shouldReceive('getConfigValueFromPath')
66
            ->andReturn([]);
67
68
        $loggerHelper = m::mock(LoggerHelper::class);
69
        $loggerHelper->shouldReceive('getName')
70
            ->andReturn('phpdocumentor_logger');
71
        $loggerHelper->shouldReceive('setHelperSet');
72
        $loggerHelper->shouldReceive('addOptions');
73
        $loggerHelper->shouldReceive('connectOutputToLogging');
74
75
        $translator = m::mock(Translator::class);
76
        $translator->shouldReceive('translate')
77
            ->zeroOrMoreTimes();
78
79
        $cache = m::mock(StorageInterface::class);
80
        $cache->shouldReceive('getOptions')
81
            ->twice()
82
            ->andReturnSelf();
83
        $cache->shouldReceive('setCacheDir');
84
        $cache->shouldReceive('getCacheDir');
85
        $cache->shouldReceive('getItem');
86
        $cache->shouldReceive('setItem');
87
        $cache->shouldReceive('getIterator')
88
        ->andReturn(new \ArrayIterator());
89
90
        $parser = m::mock(Parser::class);
91
        $parser->shouldReceive(
92
            'setForced',
93
            'setEncoding',
94
            'setMarkers',
95
            'setIgnoredTags',
96
            'setValidate',
97
            'setDefaultPackageName',
98
            'setPath',
99
            'parse'
100
        );
101
102
        $projectDescriptorBuilder = m::mock(ProjectDescriptorBuilder::class);
103
        $projectDescriptorBuilder->shouldReceive('createProjectDescriptor', 'getProjectDescriptor')
104
            ->andReturn(new ProjectDescriptor('test'));
105
106
        $command = new ParseCommand(
107
            $projectDescriptorBuilder,
108
            $parser,
109
            m::mock(FileCollector::class),
110
            $translator,
111
            $cache,
112
            new ExampleFinder(),
113
            m::mock(\phpDocumentor\Partials\Collection::class)
114
        );
115
116
117
118
        $command->setHelperSet(
119
            new HelperSet(
120
                [
0 ignored issues
show
Documentation introduced by
array($configurationHelper, $loggerHelper) is of type array<integer,object<Moc...kery\\MockInterface>"}>, but the function expects a array<integer,object<Sym...Console\Helper\Helper>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
                    $configurationHelper,
122
                    $loggerHelper
123
                ]
124
            )
125
        );
126
127
        $command->run($input, $output);
128
    }
129
}
130