Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#277)
by Jérémiah
20:41
created

GlobalVariablesPassTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 28.21 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 11
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvalidAlias() 0 19 1
A invalidAliasProvider() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Overblog\GraphQLBundle\Tests\DependencyInjection\Compiler;
4
5
use Overblog\GraphQLBundle\DependencyInjection\Compiler\GlobalVariablesPass;
6
use PHPUnit\Framework\MockObject\MockObject;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
10
class GlobalVariablesPassTest extends TestCase
11
{
12
    /**
13
     * @param mixed $invalidAlias
14
     *
15
     * @dataProvider invalidAliasProvider
16
     */
17
    public function testInvalidAlias($invalidAlias)
18
    {
19
        /** @var ContainerBuilder|MockObject $container */
20
        $container = $this->getMockBuilder(ContainerBuilder::class)
21
            ->setMethods(['findTaggedServiceIds', 'findDefinition'])
22
            ->getMock();
23
        $container->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Symfony\Component\Depend...ection\ContainerBuilder.

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...
24
            ->method('findTaggedServiceIds')
25
            ->willReturn([
26
                'my-id' => [
27
                    ['alias' => $invalidAlias],
28
                ],
29
            ]);
30
31
        $this->expectException(\InvalidArgumentException::class);
32
        $this->expectExceptionMessage('Service "my-id" tagged "overblog_graphql.global_variable" should have a valid "alias" attribute.');
33
34
        (new GlobalVariablesPass())->process($container);
0 ignored issues
show
Bug introduced by
It seems like $container defined by $this->getMockBuilder(\S...efinition'))->getMock() on line 20 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Overblog\GraphQLBundle\D...ariablesPass::process() does only seem to accept object<Symfony\Component...ction\ContainerBuilder>, 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
37 View Code Duplication
    public function invalidAliasProvider()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        return [
40
            [null],
41
            [new \stdClass()],
42
            [[]],
43
            [true],
44
            [false],
45
            [''],
46
        ];
47
    }
48
}
49