Failed Conditions
Pull Request — master (#6735)
by Matthias
92:37 queued 28:40
created

GH6443Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 6
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 entities identifier is of type "rot13" so the query parameter needs to be the converted value
42
        $this->assertSame(
43
            $this->rot13Type->convertToDatabaseValue($entity->id, $this->_em->getConnection()->getDatabasePlatform()),
44
            $lastSql['params'][0]
45
        );
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    protected function setUp(): void
52
    {
53
        parent::setUp();
54
55
        $this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\DebugStack);
56
57
        $this->_schemaTool->createSchema([
58
            $this->_em->getClassMetadata(GH6443Post::class),
59
        ]);
60
61
        $this->rot13Type = Type::getType('rot13');
62
    }
63
64
}
65
66
/** @Entity */
67
class GH6443Post
68
{
69
    /** @Id @Column(type="rot13") */
70
    public $id;
71
}
72