Failed Conditions
Pull Request — master (#16)
by Chad
01:22
created

tests/MagicPropertyDisabledTraitTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SubjectivePHPTest\Spl\Traits;
4
5
use SubjectivePHP\Spl\Traits\MagicPropertyDisabledTrait;
6
7
/**
8
 * @coversDefaultClass SubjectivePHP\Spl\Traits\MagicPropertyDisabledTrait
9
 */
10
class MagicPropertyDisabledTraitTest extends \PHPUnit\Framework\TestCase
11
{
12
    /**
13
     * @test
14
     * @covers ::__get
15
     * @expectedException \BadMethodCallException
16
     */
17
    public function magicGet()
18
    {
19
        $object = $this->getObjectForTrait(MagicPropertyDisabledTrait::class);
20
        $foo = $object->foo;
0 ignored issues
show
Accessing foo on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
$foo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
21
    }
22
23
    /**
24
     * @test
25
     * @covers ::__set
26
     * @expectedException \BadMethodCallException
27
     */
28
    public function magicSet()
29
    {
30
        $object = $this->getObjectForTrait(MagicPropertyDisabledTrait::class);
31
        $object->foo = 'bar';
0 ignored issues
show
Accessing foo on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
32
    }
33
}
34