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 Doctrine\Tests\ORM\Functional\Ticket; |
||
4 | |||
5 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
||
6 | use Doctrine\DBAL\Types as DBALTypes; |
||
7 | |||
8 | /** |
||
9 | * This test verifies that custom post-insert identifiers respect type conversion semantics. |
||
10 | * The generated identifier must be converted via DBAL types before populating the entity |
||
11 | * identifier field. |
||
12 | * |
||
13 | * @group 5935 5684 6020 6152 |
||
14 | */ |
||
15 | class DDC5684Test extends \Doctrine\Tests\OrmFunctionalTestCase |
||
16 | { |
||
17 | protected function setUp() |
||
18 | { |
||
19 | parent::setUp(); |
||
20 | |||
21 | View Code Duplication | if (DBALTypes\Type::hasType(DDC5684ObjectIdType::class)) { |
|
22 | DBALTypes\Type::overrideType(DDC5684ObjectIdType::class, DDC5684ObjectIdType::class); |
||
23 | } else { |
||
24 | DBALTypes\Type::addType(DDC5684ObjectIdType::class, DDC5684ObjectIdType::class); |
||
25 | } |
||
26 | |||
27 | $this->_schemaTool->createSchema([$this->_em->getClassMetadata(DDC5684Object::class)]); |
||
28 | } |
||
29 | |||
30 | protected function tearDown() |
||
31 | { |
||
32 | $this->_schemaTool->dropSchema([$this->_em->getClassMetadata(DDC5684Object::class)]); |
||
33 | |||
34 | parent::tearDown(); |
||
35 | } |
||
36 | |||
37 | public function testAutoIncrementIdWithCustomType() |
||
38 | { |
||
39 | $object = new DDC5684Object(); |
||
40 | $this->_em->persist($object); |
||
41 | $this->_em->flush(); |
||
42 | |||
43 | $this->assertInstanceOf(DDC5684ObjectId::class, $object->id); |
||
44 | } |
||
45 | |||
46 | public function testFetchObjectWithAutoIncrementedCustomType() |
||
47 | { |
||
48 | $object = new DDC5684Object(); |
||
49 | $this->_em->persist($object); |
||
50 | $this->_em->flush(); |
||
51 | $this->_em->clear(); |
||
52 | |||
53 | $rawId = $object->id->value; |
||
54 | $object = $this->_em->find(DDC5684Object::class, new DDC5684ObjectId($rawId)); |
||
55 | |||
56 | $this->assertInstanceOf(DDC5684ObjectId::class, $object->id); |
||
57 | $this->assertEquals($rawId, $object->id->value); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | class DDC5684ObjectIdType extends DBALTypes\IntegerType |
||
62 | { |
||
63 | public function convertToPHPValue($value, AbstractPlatform $platform) |
||
64 | { |
||
65 | return new DDC5684ObjectId($value); |
||
0 ignored issues
–
show
|
|||
66 | } |
||
67 | |||
68 | public function convertToDatabaseValue($value, AbstractPlatform $platform) |
||
69 | { |
||
70 | return $value->value; |
||
71 | } |
||
72 | |||
73 | public function getName() |
||
74 | { |
||
75 | return self::class; |
||
76 | } |
||
77 | |||
78 | public function requiresSQLCommentHint(AbstractPlatform $platform) |
||
79 | { |
||
80 | return true; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | class DDC5684ObjectId |
||
85 | { |
||
86 | public $value; |
||
87 | |||
88 | public function __construct($value) |
||
89 | { |
||
90 | $this->value = $value; |
||
91 | } |
||
92 | |||
93 | public function __toString() |
||
94 | { |
||
95 | return (string) $this->value; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @Entity |
||
101 | * @Table(name="ticket_5684_objects") |
||
102 | */ |
||
103 | class DDC5684Object |
||
104 | { |
||
105 | /** |
||
106 | * @Id |
||
107 | * @Column(type=Doctrine\Tests\ORM\Functional\Ticket\DDC5684ObjectIdType::class) |
||
108 | * @GeneratedValue(strategy="AUTO") |
||
109 | */ |
||
110 | public $id; |
||
111 | } |
||
112 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.