Failed Conditions
Pull Request — master (#6735)
by Matthias
15:07
created

GH6443Test   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

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

2 Methods

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