Skip to content
Snippets Groups Projects
Commit fcc800e9 authored by Daniel Kurowski's avatar Daniel Kurowski
Browse files

Merge branch 'deprecated-capability' into 'master'

deprecated()

See merge request !60
parents a07c388a aa311b47
No related branches found
No related tags found
1 merge request!60deprecated()
Pipeline #52235 passed
<?php declare(strict_types = 1);
namespace Grifart\ClassScaffolder\Capabilities;
use Grifart\ClassScaffolder\ClassInNamespace;
use Grifart\ClassScaffolder\Definition\ClassDefinition;
use function sprintf;
final class Deprecated implements Capability
{
/**
* @param class-string $replacement
*/
public function __construct(
private ?string $description = null,
private ?string $replacement = null,
) {}
public function applyTo(
ClassDefinition $definition,
ClassInNamespace $draft,
?ClassInNamespace $current,
): void
{
$annotation = '@deprecated';
if ($this->description !== null) {
$annotation .= ' ' . $this->description;
}
if ($this->replacement !== null) {
$annotation .= $this->description !== null ? ', ' : ' ';
$annotation .= sprintf('use {@see %s} instead', $this->replacement);
}
$draft->getClassType()->addComment($annotation);
}
}
......@@ -8,6 +8,16 @@ function constructorWithPromotedProperties(): ConstructorWithPromotedProperties
return new ConstructorWithPromotedProperties();
}
/**
* @param class-string $replacement
*/
function deprecated(
?string $description = null,
?string $replacement = null,
): Deprecated {
return new Deprecated($description, $replacement);
}
function getters(): Getters {
return new Getters();
}
......
<?php
/**
* Do not edit. This is generated file. Modify definition file instead.
*/
declare(strict_types=1);
/**
* @deprecated
*/
final class ClassName
{
}
<?php
/**
* Do not edit. This is generated file. Modify definition file instead.
*/
declare(strict_types=1);
/**
* @deprecated will be removed in v4.0.0, use {@see Grifart\ClassScaffolder\Test\Capabilities\Deprecated\Replacement} instead
*/
final class ClassName
{
}
<?php
/**
* Do not edit. This is generated file. Modify definition file instead.
*/
declare(strict_types=1);
/**
* @deprecated will be removed in v4.0.0
*/
final class ClassName
{
}
<?php
/**
* Do not edit. This is generated file. Modify definition file instead.
*/
declare(strict_types=1);
/**
* @deprecated use {@see Grifart\ClassScaffolder\Test\Capabilities\Deprecated\Replacement} instead
*/
final class ClassName
{
}
<?php declare(strict_types = 1);
namespace Grifart\ClassScaffolder\Test\Capabilities\Deprecated;
use Grifart\ClassScaffolder\ClassGenerator;
use Grifart\ClassScaffolder\Definition\ClassDefinition;
use Tester\Assert;
use Tester\TestCase;
use function Grifart\ClassScaffolder\Capabilities\deprecated;
use function Grifart\ClassScaffolder\Definition\definitionOf;
require __DIR__ . '/../../bootstrap.php';
final class DeprecatedTest extends TestCase
{
private function doAssert(
ClassDefinition $definition,
string $expectedFilePath,
): void
{
$generator = new ClassGenerator();
$phpFile = $generator->generateClass($definition);
$code = (string) $phpFile;
Assert::matchFile($expectedFilePath, $code);
}
public function testWithoutParams(): void
{
$this->doAssert(
definition: definitionOf('ClassName')
->with(deprecated()),
expectedFilePath: __DIR__ . '/DeprecatedTest.expected.onlyAnnotation.phps',
);
}
public function testWithDescription(): void
{
$this->doAssert(
definition: definitionOf('ClassName')
->with(deprecated(description: 'will be removed in v4.0.0')),
expectedFilePath: __DIR__ . '/DeprecatedTest.expected.withDescription.phps',
);
}
public function testWithReplacement(): void
{
$this->doAssert(
definition: definitionOf('ClassName')
->with(deprecated(replacement: Replacement::class)),
expectedFilePath: __DIR__ . '/DeprecatedTest.expected.withReplacement.phps',
);
}
public function testWithAll(): void
{
$this->doAssert(
definition: definitionOf('ClassName')
->with(deprecated(
description: 'will be removed in v4.0.0',
replacement: Replacement::class,
)),
expectedFilePath: __DIR__ . '/DeprecatedTest.expected.withAll.phps',
);
}
}
(new DeprecatedTest())->run();
<?php declare(strict_types = 1);
namespace Grifart\ClassScaffolder\Test\Capabilities\Deprecated;
final class Replacement
{}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment