TraitAssemblerTest::testAssembleTraitWithMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
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-2018 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\Descriptor\Builder\Reflector;
14
15
use Mockery as m;
16
use phpDocumentor\Descriptor\MethodDescriptor;
17
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
18
use phpDocumentor\Reflection\Fqsen;
19
use phpDocumentor\Reflection\Php\Method;
20
use phpDocumentor\Reflection\Php\Trait_;
21
22
/**
23
 * @coversDefaultClass \phpDocumentor\Descriptor\Builder\Reflector\TraitAssembler
24
 * @covers ::<private>
25
 */
26
class TraitAssemblerTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
27
{
28
    /**
29
     * @covers ::create
30
     */
31
    public function testAssembleTraitWithMethod()
32
    {
33
        $method = new MethodDescriptor();
34
        $method->setName('method');
35
        $builder = m::mock(ProjectDescriptorBuilder::class);
36
        $builder->shouldReceive('buildDescriptor')->andReturn($method);
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...
37
38
        $traitFqsen = new Fqsen('\My\Space\MyTrait');
39
        $trait = new Trait_($traitFqsen);
40
        $trait->addMethod(new Method(new Fqsen('\My\Space\MyTrait::method()')));
41
        $assembler = new TraitAssembler();
42
        $assembler->setBuilder($builder);
43
44
        $result = $assembler->create($trait);
45
46
        static::assertEquals('\My\Space', $result->getNamespace());
47
        static::assertSame($traitFqsen, $result->getFullyQualifiedStructuralElementName());
48
        static::assertEquals('MyTrait', $result->getName());
49
        static::assertInstanceOf(MethodDescriptor::class, $result->getMethods()->get('method', false));
50
    }
51
}
52