GitPackageTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
A makeSignatureCheck() 0 10 1
A testWillReportSuccessfulVerification() 0 20 1
B testWillReportSuccessfulVerificationWithMultipleChecks() 0 27 1
A testWillReportFailedVerification() 0 20 1
B testWillReportFailedVerificationWithMultipleChecks() 0 29 1
A testWillReportSuccessfulVerificationWithMultipleChecksAndSuccessfulOneNotFirst() 0 23 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\Git\GitSignatureCheck;
10
use Roave\ComposerGpgVerify\Package\GitPackage;
11
12
/**
13
 * @covers \Roave\ComposerGpgVerify\Package\GitPackage
14
 */
15
final class GitPackageTest extends TestCase
16
{
17
    /**
18
     * @var PackageInterface
19
     */
20
    private $package;
21
22
    /**
23
     * @var string
24
     */
25
    private $packageName;
26
27
    protected function setUp() : void
28
    {
29
        parent::setUp();
30
31
        $this->packageName = uniqid('packageName', true);
32
33
        /* @var $package PackageInterface|\PHPUnit_Framework_MockObject_MockObject */
34
        $package = $this->createMock(PackageInterface::class);
35
36
        $package->expects(self::any())->method('getName')->willReturn($this->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...
37
38
        $this->package = $package;
0 ignored issues
show
Documentation Bug introduced by
It seems like $package can also be of type object<PHPUnit_Framework_MockObject_MockObject>. However, the property $package is declared as type object<Composer\Package\PackageInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
39
    }
40
41
    public function testWillReportSuccessfulVerification() : void
42
    {
43
        $verification = GitPackage::fromPackageAndSignatureChecks(
44
            $this->package,
45
            $this->makeSignatureCheck(true, 'yadda')
46
        );
47
48
        self::assertInstanceOf(GitPackage::class, $verification);
49
        self::assertSame($this->packageName, $verification->packageName());
50
        self::assertTrue($verification->isVerified());
51
        self::assertSame(
52
            <<<REASON
53
The following GIT GPG signature checks passed for package "{$this->packageName}":
54
55
yadda
56
REASON
57
            ,
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
58
            $verification->printReason()
59
        );
60
    }
61
62
    public function testWillReportSuccessfulVerificationWithMultipleChecks() : void
63
    {
64
        $verification = GitPackage::fromPackageAndSignatureChecks(
65
            $this->package,
66
            $this->makeSignatureCheck(true, 'yadda'),
67
            $this->makeSignatureCheck(true, 'dadda'),
68
            $this->makeSignatureCheck(true, 'wadda'),
69
            $this->makeSignatureCheck(false, 'nope')
70
        );
71
72
        self::assertInstanceOf(GitPackage::class, $verification);
73
        self::assertSame($this->packageName, $verification->packageName());
74
        self::assertTrue($verification->isVerified());
75
        self::assertSame(
76
            <<<REASON
77
The following GIT GPG signature checks passed for package "{$this->packageName}":
78
79
yadda
80
81
dadda
82
83
wadda
84
REASON
85
            ,
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
86
            $verification->printReason()
87
        );
88
    }
89
90
    public function testWillReportFailedVerification() : void
91
    {
92
        $verification = GitPackage::fromPackageAndSignatureChecks(
93
            $this->package,
94
            $this->makeSignatureCheck(false, 'yadda')
95
        );
96
97
        self::assertInstanceOf(GitPackage::class, $verification);
98
        self::assertSame($this->packageName, $verification->packageName());
99
        self::assertFalse($verification->isVerified());
100
        self::assertSame(
101
            <<<REASON
102
The following GIT GPG signature checks have failed for package "{$this->packageName}":
103
104
yadda
105
REASON
106
            ,
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
107
            $verification->printReason()
108
        );
109
    }
110
111
    public function testWillReportFailedVerificationWithMultipleChecks() : void
112
    {
113
        $verification = GitPackage::fromPackageAndSignatureChecks(
114
            $this->package,
115
            $this->makeSignatureCheck(false, 'yadda'),
116
            $this->makeSignatureCheck(false, 'dadda'),
117
            $this->makeSignatureCheck(false, 'wadda'),
118
            $this->makeSignatureCheck(false, 'nope')
119
        );
120
121
        self::assertInstanceOf(GitPackage::class, $verification);
122
        self::assertSame($this->packageName, $verification->packageName());
123
        self::assertFalse($verification->isVerified());
124
        self::assertSame(
125
            <<<REASON
126
The following GIT GPG signature checks have failed for package "{$this->packageName}":
127
128
yadda
129
130
dadda
131
132
wadda
133
134
nope
135
REASON
136
            ,
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
137
            $verification->printReason()
138
        );
139
    }
140
141
    public function testWillReportSuccessfulVerificationWithMultipleChecksAndSuccessfulOneNotFirst() : void
142
    {
143
        $verification = GitPackage::fromPackageAndSignatureChecks(
144
            $this->package,
145
            $this->makeSignatureCheck(false, 'yadda'),
146
            $this->makeSignatureCheck(false, 'dadda'),
147
            $this->makeSignatureCheck(false, 'wadda'),
148
            $this->makeSignatureCheck(true, 'yarp')
149
        );
150
151
        self::assertInstanceOf(GitPackage::class, $verification);
152
        self::assertSame($this->packageName, $verification->packageName());
153
        self::assertTrue($verification->isVerified());
154
        self::assertSame(
155
            <<<REASON
156
The following GIT GPG signature checks passed for package "{$this->packageName}":
157
158
yarp
159
REASON
160
            ,
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
161
            $verification->printReason()
162
        );
163
    }
164
165
    private function makeSignatureCheck(bool $passed, string $reasoning) : GitSignatureCheck
166
    {
167
        /* @var $verification GitSignatureCheck|\PHPUnit_Framework_MockObject_MockObject */
168
        $verification = $this->createMock(GitSignatureCheck::class);
169
170
        $verification->expects(self::any())->method('asHumanReadableString')->willReturn($reasoning);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Roave\ComposerGpgVerify\...e\Git\GitSignatureCheck.

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...
171
        $verification->expects(self::any())->method('canBeTrusted')->willReturn($passed);
172
173
        return $verification;
174
    }
175
}
176