Failed Conditions
Pull Request — master (#6735)
by Matthias
19:41
created

GH6443Test::testIssue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 25
rs 8.8571
c 2
b 0
f 1
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\DBAL\Types\Type;
6
use Doctrine\Tests\DbalTypes\Rot13Type;
7
use Doctrine\Tests\OrmFunctionalTestCase;
8
9
/**
10
 * @group 6443
11
 */
12
class GH6443Test extends OrmFunctionalTestCase
13
{
14
15
    /**
16
     * @var Rot13Type
17
     */
18
    private $rot13Type;
19
20
    /**
21
     * when having an entity, that has a non scalar identifier, the type will not be guessed / converted correctly
22
     */
23
    public function testIssue()
24
    {
25
26
        $entity = new GH6443Post();
27
        $entity->id = 'Foo';
28
29
        $dql = 'SELECT p FROM ' . GH6443Post::class . ' p WHERE p = ?1';
30
        $query = $this->_em->createQuery($dql);
31
32
        // we do not know that the internal type is a rot13, so we can not add the type parameter here
33
        $query->setParameter(1, $entity);
34
35
        // we do not need the result, but we need to execute it to log the SQL-Statement
36
        $result = $query->getResult();
0 ignored issues
show
Unused Code introduced by
$result 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...
37
38
        $sqlLogger = $this->_em->getConnection()->getConfiguration()->getSQLLogger();
39
        $lastSql = $sqlLogger->queries[count($sqlLogger->queries)];
0 ignored issues
show
Bug introduced by
Accessing queries on the interface Doctrine\DBAL\Logging\SQLLogger 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...
40
41
        // the entity's identifier is of type "rot13" so the query parameter needs to be this type too
42
        $this->assertSame(
43
            $this->rot13Type->getName(),
44
            $lastSql['types'][0],
45
            "asserting that the entity's identifier type is correctly inferred"
46
        );
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    protected function setUp(): void
53
    {
54
        parent::setUp();
55
56
        $this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\DebugStack);
57
58
        $this->_schemaTool->createSchema([
59
            $this->_em->getClassMetadata(GH6443Post::class),
60
        ]);
61
62
        $this->rot13Type = Type::getType('rot13');
63
    }
64
65
}
66
67
/** @Entity */
68
class GH6443Post
69
{
70
    /** @Id @Column(type="rot13") */
71
    public $id;
72
}
73