UnknownPackageFormatTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A testVerification() 0 21 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ComposerGpgVerify\Package;
6
7
use Composer\Package\PackageInterface;
8
use PHPUnit\Framework\TestCase;
9
use Roave\ComposerGpgVerify\Package\UnknownPackageFormat;
10
11
/**
12
 * @covers \Roave\ComposerGpgVerify\Package\UnknownPackageFormat
13
 */
14
final class UnknownPackageFormatTest extends TestCase
15
{
16
    public function testVerification() : void
17
    {
18
        /* @var $package PackageInterface|\PHPUnit_Framework_MockObject_MockObject */
19
        $package     = $this->createMock(PackageInterface::class);
20
        $packageName = uniqid('packageName', true);
21
22
        $package->expects(self::any())->method('getName')->willReturn($packageName);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Composer\Package\PackageInterface.

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...
23
24
        $verification = UnknownPackageFormat::fromNonGitPackage($package);
0 ignored issues
show
Bug introduced by
It seems like $package defined by $this->createMock(\Compo...ackageInterface::class) on line 19 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\ComposerGpgVerify\...at::fromNonGitPackage() does only seem to accept object<Composer\Package\PackageInterface>, 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...
25
26
        self::assertInstanceOf(UnknownPackageFormat::class, $verification);
27
        self::assertSame($packageName, $verification->packageName());
28
        self::assertFalse($verification->isVerified(), 'Unknown package types cannot be verified');
29
        self::assertSame(
30
            'Package "'
31
            . $packageName
32
            . '" is in a format that Roave\ComposerGpgVerify cannot verify:'
33
            . ' try forcing it to be downloaded as GIT repository',
34
            $verification->printReason()
35
        );
36
    }
37
}
38