8000 [ErrorHandler] trigger deprecations for `@final` properties by nicolas-grekas · Pull Request #45360 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[ErrorHandler] trigger deprecations for @final properties #45360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion UPGRADE-6.1.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
UPGRADE FROM 6.0 to 6.1
=======================

All components
--------------

* Public and protected properties are now considered final;
instead of overriding a property, consider setting its value in the constructor

Console
-------

* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead.
* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead

Serializer
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public function testTooLongNamespace()

abstract class MaxIdLengthAdapter extends AbstractAdapter
{
protected $maxIdLength = 50;

public function __construct(string $ns)
{
$this->maxIdLength = 50;

parent::__construct($ns);
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
---

* Add method `__toString()` to `InputInterface`
* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead.
* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead

6.0
---
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,27 @@ class Php8Command2 extends Command

class MyCommand extends Command
{
/**
* @deprecated since Symfony 6.1
*/
protected static $defaultName = 'my:command';

/**
* @deprecated since Symfony 6.1
*/
protected static $defaultDescription = 'This is a command I wrote all by myself';
}

#[AsCommand(name: 'my:command', description: 'This is a command I wrote all by myself')]
class MyAnnotatedCommand extends Command
{
/**
* @deprecated since Symfony 6.1
*/
protected static $defaultName = 'i-shall-be-ignored';

/**
* @deprecated since Symfony 6.1
*/
protected static $defaultDescription = 'This description should be ignored.';
}
19 changes: 9 additions & 10 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,6 @@ class ProjectServiceContainer extends Container
public $__foo_bar;
public $__foo_baz;
public $__internal;
protected $privates;
protected $methodMap = [
'bar' => 'getBarService',
'foo_bar' => 'getFooBarService',
'foo.baz' => 'getFoo_BazService',
'circular' => 'getCircularService',
'throw_exception' => 'getThrowExceptionService',
'throws_exception_on_service_configuration' => 'getThrowsExceptionOnServiceConfigurationService',
'internal_dependency' => 'getInternalDependencyService',
];

public function __construct()
{
Expand All @@ -434,6 +424,15 @@ public function __construct()
$this->__internal = new \stdClass();
$this->privates = [];
$this->aliases = ['alias' => 'bar'];
$this->methodMap = [
'bar' => 'getBarService',
'foo_bar' => 'getFooBarService',
'foo.baz' => 'getFoo_BazService',
'circular' => 'getCircularService',
'throw_exception' => 'getThrowExceptionService',
'throws_exception_on_service_configuration' => 'getThrowsExceptionOnServiceConfigurationService',
'internal_dependency' => 'getInternalDependencyService',
];
}

protected function getInternalService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testRegisterClasses()
$container = new ContainerBuilder();
$container->setParameter('sub_dir', 'Sub');
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
$loader->autoRegisterAliasesForSinglyImplementedInterfaces = false;
$loader->noAutoRegisterAliasesForSinglyImplementedInterfaces();

$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*');
$loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*'); // loading twice should not be an issue
Expand Down Expand Up @@ -270,7 +270,10 @@ public function testRegisterClassesWithWhenEnv(?string $env, bool $expected)

class TestFileLoader extends FileLoader
{
public $autoRegisterAliasesForSinglyImplementedInterfaces = true;
public function noAutoRegisterAliasesForSinglyImplementedInterfaces()
{
$this->autoRegisterAliasesForSinglyImplementedInterfaces = false;
}

public function load(mixed $resource, string $type = null): mixed
{
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/ErrorHandler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG
6.1
---

* Report overridden `@final` constants
* Report overridden `@final` constants and properties

5.4
---
Expand Down
35 changes: 22 additions & 13 deletions src/Symfony/Component/ErrorHandler/DebugClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class DebugClassLoader
private static array $checkedClasses = [];
private static array $final = [];
private static array $finalMethods = [];
private static array $finalProperties = [];
private static array $finalConstants = [];
private static array $deprecated = [];
private static array $internal = [];
Expand Down Expand Up @@ -469,9 +470,10 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
self::$finalMethods[$class] = [];
self::$internalMethods[$class] = [];
self::$annotatedParameters[$class] = [];
self::$finalProperties[$class] = [];
self::$finalConstants[$class] = [];
foreach ($parentAndOwnInterfaces as $use) {
foreach (['finalMethods', 'internalMethods', 'annotatedParameters', 'returnTypes', 'finalConstants'] as $property) {
foreach (['finalMethods', 'internalMethods', 'annotatedParameters', 'returnTypes', 'finalProperties', 'finalConstants'] as $property) {
if (isset(self::${$property}[$use])) {
self::${$property}[$class] = self::${$property}[$class] ? self::${$property}[$use] + self::${$property}[$class] : self::${$property}[$use];
}
Expand Down Expand Up @@ -626,22 +628,29 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
}
}

foreach ($refl->getReflectionConstants(\ReflectionClassConstant::IS_PUBLIC | \ReflectionClassConstant::IS_PROTECTED) as $constant) {
if ($constant->class !== $class) {
continue;
}
$finals = isset(self::$final[$class]) || $refl->isFinal() ? [] : [
'finalConstants' => $refl->getReflectionConstants(\ReflectionClassConstant::IS_PUBLIC | \ReflectionClassConstant::IS_PROTECTED),
'finalProperties' => $refl->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED),
];
foreach ($finals as $type => $reflectors) {
foreach ($reflectors as $r) {
if ($r->class !== $class) {
continue;
}

$doc = $this->parsePhpDoc($r);

foreach ($parentAndOwnInterfaces as $use) {
if (isset(self::$finalConstants[$use][$constant->name])) {
$deprecations[] = sprintf('The "%s::%s" constant is considered final. You should not override it in "%s".', self::$finalConstants[$use][$constant->name], $constant->name, $class);
foreach ($parentAndOwnInterfaces as $use) {
if (isset(self::${$type}[$use][$r->name]) && !isset($doc['deprecated']) && ('finalConstants' === $type || substr($use, 0, strrpos($use, '\\')) !== substr($use, 0, strrpos($class, '\\')))) {
$msg = 'finalConstants' === $type ? '%s" constant' : '$%s" property';
$deprecations[] = sprintf('The "%s::'.$msg.' is considered final. You should not override it in "%s".', self::${$type}[$use][$r->name], $r->name, $class);
}
}
}

if (!($doc = $this->parsePhpDoc($constant)) || !isset($doc['final'])) {
continue;
if (isset($doc['final']) || ('finalProperties' === $type && str_starts_with($class, 'Symfony\\') && !$r->hasType())) {
self::${$type}[$class][$r->name] = $class;
}
}

self::$finalConstants[$class][$constant->name] = $class;
}

return $deprecations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,34 @@ class_exists('Test\\'.ReturnType::class, true);
], $deprecations);
}

public function testOverrideFinalProperty()
{
$deprecations = [];
set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
$e = error_reporting(E_USER_DEPRECATED);

class_exists(Fixtures\OverrideFinalProperty::class, true);
class_exists(Fixtures\FinalProperty\OverrideFinalPropertySameNamespace::class, true);
class_exists('Test\\'.OverrideOutsideFinalProperty::class, true);

error_reporting($e);
restore_error_handler();

$this->assertSame([
'The "Symfony\Component\ErrorHandler\Tests\Fixtures\FinalProperty\FinalProperty::$pub" property is considered final. You should not override it in "Symfony\Component\ErrorHandler\Tests\Fixtures\OverrideFinalProperty".',
'The "Symfony\Component\ErrorHandler\Tests\Fixtures\FinalProperty\FinalProperty::$prot" property is considered final. You should not override it in "Symfony\Component\ErrorHandler\Tests\Fixtures\OverrideFinalProperty".',
'The "Symfony\Component\ErrorHandler\Tests\Fixtures\FinalProperty\FinalProperty::$implicitlyFinal" property is considered final. You should not override it in "Symfony\Component\ErrorHandler\Tests\Fixtures\OverrideFinalProperty".',
'The "Test\Symfony\Component\ErrorHandler\Tests\FinalProperty\OutsideFinalProperty::$final" property is considered final. You should not override it in "Test\Symfony\Component\ErrorHandler\Tests\OverrideOutsideFinalProperty".'
], $deprecations);
}

public function testOverrideFinalConstant()
{
$deprecations = [];
set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
$e = error_reporting(E_USER_DEPRECATED);

class_exists( Fixtures\FinalConstant\OverrideFinalConstant::class, true);
class_exists(Fixtures\FinalConstant\OverrideFinalConstant::class, true);

error_reporting($e);
restore_error_handler();
Expand Down Expand Up @@ -523,6 +544,10 @@ public function ownAbstractBaseMethod() { }
return $fixtureDir.\DIRECTORY_SEPARATOR.'ReturnType.php';
} elseif ('Test\\'.Fixtures\OutsideInterface::class === $class) {
return $fixtureDir.\DIRECTORY_SEPARATOR.'OutsideInterface.php';
} elseif ('Test\\'.OverrideOutsideFinalProperty::class === $class) {
return $fixtureDir.'OverrideOutsideFinalProperty.php';
} elseif ('Test\\Symfony\\Component\\ErrorHandler\\Tests\\FinalProperty\\OutsideFinalProperty' === $class) {
return $fixtureDir.'FinalProperty'.\DIRECTORY_SEPARATOR.'OutsideFinalProperty.php';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Symfony\Component\ErrorHandler\Tests\Fixtures\FinalProperty;

class FinalProperty
{
/**
* @final
*/
public $pub;

/**
* @final
*/
protected $prot;

/**
* @final
*/
private $priv;

/**
* @final
*/
public $notOverriden;

protected $implicitlyFinal;

protected string $typedSoNotFinal;

protected $deprecated;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Test\Symfony\Component\ErrorHandler\Tests\FinalProperty;

class OutsideFinalProperty
{
/**
* @final
*/
public $final;

protected $notImplicitlyFinalBecauseNotInSymfony;

/**
* @final
*/
public $notOverriden;

/**
* @final
*/
protected $deprecated;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\ErrorHandler\Tests\Fixtures\FinalProperty;

class OverrideFinalPropertySameNamespace extends FinalProperty
{
public $pub;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Component\ErrorHandler\Tests\Fixtures;

use Symfony\Component\ErrorHandler\Tests\Fixtures\FinalProperty\FinalProperty;

class OverrideFinalProperty extends FinalProperty
{
public $pub;
protected $prot;
private $priv;
protected $implicitlyFinal;
protected string $typedSoNotFinal;
/**
* @deprecated
*/
protected $deprecated;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Test\Symfony\Component\ErrorHandler\Tests;

use Test\Symfony\Component\ErrorHandler\Tests\FinalProperty\OutsideFinalProperty;

class OverrideOutsideFinalProperty extends OutsideFinalProperty
{
public $final;
protected $notImplicitlyFinalBecauseNotInSymfony;
/**
* @deprecated
*/
protected $deprecated;
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/Tests/VersionAwareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

trait VersionAwareTest
{
protected static $supportedFeatureSetVersion = 404;
protected static int $supportedFeatureSetVersion = 404;

protected function requiresFeatureSet(int $requiredFeatureSetVersion)
{
Expand Down
3 changes: 0 additions & 3 deletions src/Symfony/Component/Ldap/Adapter/ExtLdap/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class Query extends AbstractQuery
{
public const PAGINATION_OID = \LDAP_CONTROL_PAGEDRESULTS;

/** @var Connection */
protected $connection;

/** @var resource[]|Result[] */
private array $results;

Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Validator/Test/ConstraintValidatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
* A test case to ease testing Constraint Validators.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @template T of ConstraintValidatorInterface
*/
abstract class ConstraintValidatorTestCase extends TestCase
{
Expand All @@ -48,7 +50,7 @@ abstract class ConstraintValidatorTestCase extends TestCase
protected $context;

/**
* @var ConstraintValidatorInterface
* @var T
*/
protected $validator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@

/**
* @requires extension fileinfo
*
* @extends ConstraintValidatorTestCase<ImageValidator>
*/
class ImageValidatorTest extends ConstraintValidatorTestCase
{
protected $context;

/**
* @var ImageValidator
*/
protected $validator;

protected $path;
protected $image;
protected $imageLandscape;
Expand Down
Loading
0