Skip to content

Commit f4502e6

Browse files
committed
obsolete annotation
1 parent 6113c42 commit f4502e6

File tree

9 files changed

+217
-2
lines changed

9 files changed

+217
-2
lines changed

src/Annotation/Obsolete.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Annotation;
6+
7+
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
8+
9+
/**
10+
* Used to safely drop column from table which used in several services.For safely drop column from table which used in several services.
11+
* The property should not be displayed in schema but should remain in the tableThe property should not be in schema.
12+
*
13+
* @Annotation
14+
* @NamedArgumentConstructor
15+
* @Target({"PROPERTY"})
16+
*/
17+
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
18+
class Obsolete
19+
{
20+
}

src/Configurator.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Cycle\Annotated\Annotation\Entity;
1010
use Cycle\Annotated\Annotation\ForeignKey;
1111
use Cycle\Annotated\Annotation\GeneratedValue;
12+
use Cycle\Annotated\Annotation\Obsolete;
1213
use Cycle\Annotated\Annotation\Relation as RelationAnnotation;
1314
use Cycle\Annotated\Exception\AnnotationException;
1415
use Cycle\Annotated\Exception\AnnotationRequiredArgumentsException;
@@ -96,7 +97,7 @@ public function initEmbedding(Embeddable $emb, \ReflectionClass $class): EntityS
9697

9798
public function initFields(EntitySchema $entity, \ReflectionClass $class, string $columnPrefix = ''): void
9899
{
99-
foreach ($class->getProperties() as $property) {
100+
foreach ($this->getActualProperties($class) as $property) {
100101
try {
101102
$column = $this->reader->firstPropertyMetadata($property, Column::class);
102103
} catch (\Exception $e) {
@@ -119,7 +120,7 @@ public function initFields(EntitySchema $entity, \ReflectionClass $class, string
119120

120121
public function initRelations(EntitySchema $entity, \ReflectionClass $class): void
121122
{
122-
foreach ($class->getProperties() as $property) {
123+
foreach ($this->getActualProperties($class) as $property) {
123124
$metadata = $this->getPropertyMetadata($property, RelationAnnotation\RelationInterface::class);
124125

125126
foreach ($metadata as $meta) {
@@ -413,4 +414,22 @@ private function isOnInsertGeneratedField(Field $field): bool
413414
default => $field->isPrimary(),
414415
};
415416
}
417+
418+
/**
419+
* @return \Generator<\ReflectionProperty>
420+
*/
421+
private function getActualProperties(\ReflectionClass $class): \Generator
422+
{
423+
foreach ($class->getProperties() as $property) {
424+
// Obsolete property must not be included in the scheme.
425+
$metadata = \iterator_to_array(
426+
$this->getPropertyMetadata($property, Obsolete::class),
427+
);
428+
if ([] !== $metadata) {
429+
continue;
430+
}
431+
432+
yield $property;
433+
}
434+
}
416435
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Fixtures\Fixtures26;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\Annotated\Annotation\Obsolete;
10+
11+
/**
12+
* @Entity(table="user")
13+
*/
14+
#[Entity(table: 'user')]
15+
class User
16+
{
17+
/** @Column(type="primary") */
18+
#[Column(type: 'primary')]
19+
protected $id;
20+
21+
/** @Column(type="string") */
22+
#[Column(type: 'string')]
23+
protected $name;
24+
25+
/**
26+
* @Column(type="integer", nullable=true)
27+
* @Obsolete
28+
*
29+
* @deprecated Since May 5, 2025
30+
*/
31+
#[Obsolete]
32+
#[Column(type: 'string', nullable: true)]
33+
protected $skype = null;
34+
35+
/**
36+
* There is must not be problems with it.
37+
*/
38+
#[Obsolete]
39+
private $secret;
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Functional\Driver\Common;
6+
7+
use Cycle\Annotated\Entities;
8+
use Cycle\Annotated\Locator\TokenizerEntityLocator;
9+
use Cycle\ORM\SchemaInterface;
10+
use Cycle\Schema\Compiler;
11+
use Cycle\Schema\Generator\RenderTables;
12+
use Cycle\Schema\Generator\SyncTables;
13+
use Cycle\Schema\Registry;
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
use Spiral\Attributes\ReaderInterface;
16+
use Spiral\Tokenizer\Config\TokenizerConfig;
17+
use Spiral\Tokenizer\Tokenizer;
18+
19+
abstract class ObsoleteTest extends BaseTestCase
20+
{
21+
#[DataProvider('allReadersProvider')]
22+
public function testObsoleteColumn(ReaderInterface $reader): void
23+
{
24+
$tokenizer = new Tokenizer(
25+
new TokenizerConfig([
26+
'directories' => [__DIR__ . '/../../../Fixtures/Fixtures26'],
27+
'exclude' => [],
28+
])
29+
);
30+
31+
$locator = $tokenizer->classLocator();
32+
33+
$r = new Registry($this->dbal);
34+
35+
$schema = (new Compiler())->compile($r, [
36+
new Entities(new TokenizerEntityLocator($locator, $reader), $reader),
37+
new RenderTables(),
38+
new SyncTables(),
39+
]);
40+
41+
$this->assertArrayNotHasKey('skype', $schema['user'][SchemaInterface::COLUMNS]);
42+
$this->assertArrayNotHasKey('skype', $schema['user'][SchemaInterface::TYPECAST]);
43+
}
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Functional\Driver\MySQL;
6+
7+
// phpcs:ignore
8+
use Cycle\Annotated\Tests\Functional\Driver\Common\ObsoleteTest as CommonClass;
9+
10+
/**
11+
* @group driver
12+
* @group driver-mysql
13+
*/
14+
class ObsoleteTest extends CommonClass
15+
{
16+
public const DRIVER = 'mysql';
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Functional\Driver\Postgres;
6+
7+
// phpcs:ignore
8+
use Cycle\Annotated\Tests\Functional\Driver\Common\ObsoleteTest as CommonClass;
9+
10+
/**
11+
* @group driver
12+
* @group driver-postgres
13+
*/
14+
class ObsoleteTest extends CommonClass
15+
{
16+
public const DRIVER = 'postgres';
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Functional\Driver\SQLServer;
6+
7+
// phpcs:ignore
8+
use Cycle\Annotated\Tests\Functional\Driver\Common\ObsoleteTest as CommonClass;
9+
10+
/**
11+
* @group driver
12+
* @group driver-sqlserver
13+
*/
14+
class ObsoleteTest extends CommonClass
15+
{
16+
public const DRIVER = 'sqlserver';
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Functional\Driver\SQLite;
6+
7+
// phpcs:ignore
8+
use Cycle\Annotated\Tests\Functional\Driver\Common\ObsoleteTest as CommonClass;
9+
10+
/**
11+
* @group driver
12+
* @group driver-sqlite
13+
*/
14+
class ObsoleteTest extends CommonClass
15+
{
16+
public const DRIVER = 'sqlite';
17+
}

tests/generate.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
<?php
22

3+
/**
4+
* 1. Create new test case class in folder `tests/Functional/Driver/Common` with content:
5+
*
6+
*
7+
* namespace Cycle\Annotated\Tests\Functional\Driver\Common;
8+
*
9+
* abstract class MyTest extends BaseTest {}
10+
*
11+
*
12+
* 2. run this script
13+
*
14+
*
15+
* php generate.php
16+
*
17+
*
18+
* 3. Don't forget commit newly created files:
19+
*
20+
* - `tests/Functional/Driver/MySQL/MyTest`
21+
* - `tests/Functional/Driver/Postgres/MyTest`
22+
* - `tests/Functional/Driver/SQLite/MyTest`
23+
* - `tests/Functional/Driver/SQLServer/MyTest`
24+
*/
25+
326
declare(strict_types=1);
427

28+
use Cycle\Annotated\Tests\Functional\Driver\Common\BaseTest;
529
use Spiral\Tokenizer;
630

731
error_reporting(E_ALL | E_STRICT);

0 commit comments

Comments
 (0)