8000 Never rely on dynamic properties by nicolas-grekas · Pull Request #44037 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Never rely on dynamic properties #44037

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 1 commit into from
Nov 15, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ function ($definition) {

final class DummyClass implements DummyInterface, SunnyInterface
{
private $ref;

public function dummy()
{
return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ public function testGetServiceIds()
{
$builder = new ContainerBuilder();
$builder->register('foo', 'stdClass');
$builder->bar = $bar = new \stdClass();
$builder->register('bar', 'stdClass');
$this->assertEquals(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

final class StdClassDecorator
{
public $foo;

public function __construct(\stdClass $foo)
{
$this->foo = $foo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public static function create($config)

class FoobarCircular
{
public $foo;

public function __construct(FooCircular $foo)
{
$this->foo = $foo;
Expand All @@ -150,6 +152,8 @@ public function __construct(FooCircular $foo)

class FooCircular
{
public $bar;

public function __construct(BarCircular $bar)
{
$this->bar = $bar;
Expand All @@ -158,6 +162,8 @@ public function __construct(BarCircular $bar)

class BarCircular
{
public $foobar;

public function addFoobar(FoobarCircular $foobar)
{
$this->foobar = $foobar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ public function __invoke()

class TestEventListener
{
public $name;
public $preFooInvoked = false;
public $postFooInvoked = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class KernelForTest extends Kernel
{
public function getBundleMap()
{
return $this->bundleMap;
return [];
}

public function registerBundles(): iterable
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mime/Part/DataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
class DataPart extends TextPart
{
/** @internal */
protected $_parent;

private static $mimeTypes;

private $filename;
Expand All @@ -32,6 +35,8 @@ class DataPart extends TextPart
*/
public function __construct($body, string $filename = null, string $contentType = null, string $encoding = null)
{
unset($this->_parent);

if (null === $contentType) {
$contentType = 'application/octet-stream';
}
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mime/Part/SMimePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
class SMimePart extends AbstractPart
{
/** @internal */
protected $_headers;

private $body;
private $type;
private $subtype;
Expand All @@ -28,6 +31,8 @@ class SMimePart extends AbstractPart
*/
public function __construct($body, string $type, string $subtype, array $parameters)
{
unset($this->_headers);

parent::__construct();

if (!\is_string($body) && !is_iterable($body)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mime/Part/TextPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
class TextPart extends AbstractPart
{
/** @internal */
protected $_headers;

private static $encoders = [];

private $body;
Expand All @@ -37,6 +40,8 @@ class TextPart extends AbstractPart
*/
public function __construct($body, ?string $charset = 'utf-8', string $subtype = 'plain', string $encoding = null)
{
unset($this->_headers);

parent::__construct();

if (!\is_string($body) && !\is_resource($body)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@
*/
class AuthenticationException extends RuntimeException
{
/** @internal */
protected $serialized;

private $token;

public function __construct(string $message = '', int $code = 0, \Throwable $previous = null)
{
unset($this->serialized);
parent::__construct($message, $code, $previous);
}

/**
* Get the token.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

class ChildCustomUserMessageAuthenticationException extends CustomUserMessageAuthenticationException
{
public $childMember;

public function __serialize(): array
{
return [$this->childMember, parent::__serialize()];
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ CHANGELOG
3.2.0
-----

* deprecated `Tests\Constraints\AbstractContraintValidatorTest` in favor of `Test\ConstraintValidatorTestCase`
* deprecated `Tests\Constraints\AbstractConstraintValidatorTest` in favor of `Test\ConstraintValidatorTestCase`
* added support for PHP constants in YAML configuration files

3.1.0
Expand Down
14 changes: 9 additions & 5 deletions src/Symfony/Component/Validator/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*
* Constraint instances are immutable and serializable.
*
* @property string[] $groups The groups that the constraint belongs to
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class Constraint
Expand Down Expand Up @@ -58,6 +56,13 @@ abstract class Constraint
*/
public $payload;

/**
* The groups that the constraint belongs to.
*
* @var string[]
*/
public $groups;

/**
* Returns the name of the given error code.
*
Expand Down Expand Up @@ -105,14 +110,13 @@ public static function getErrorName($errorCode)
*/
public function __construct($options = null)
{
unset($this->groups); // enable lazy initialization

$defaultOption = $this->getDefaultOption();
$invalidOptions = [];
$missingOptions = array_flip((array) $this->getRequiredOptions());
$knownOptions = get_class_vars(static::class);

// The "groups" option is added to the object lazily
$knownOptions['groups'] = true;

if (\is_array($options) && isset($options['value']) && !property_exists($this, 'value')) {
if (null === $defaultOption) {
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', static::class));
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Validator/Constraints/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __construct($options = null)
}
}

if (!property_exists($this, 'groups')) {
if (!isset(((array) $this)['groups'])) {
$mergedGroups = [];

foreach ($nestedConstraints as $constraint) {
Expand All @@ -96,7 +96,7 @@ public function __construct($options = null)
}

foreach ($nestedConstraints as $constraint) {
if (property_exists($constraint, 'groups')) {
if (isset(((array) $constraint)['groups'])) {
$excessGroups = array_diff($constraint->groups, $this->groups);

if (\count($excessGroups) > 0) {
Expand Down Expand Up @@ -141,7 +141,7 @@ abstract protected function getCompositeOption();
*
* @return Constraint[]
*/
public function getNestedContraints()
public function getNestedConstraints()
{
/* @var Constraint[] $nestedConstraints */
return $this->{$this->getCompositeOption()};
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ private function checkConstraint(Constraint $constraint)
}

if ($constraint instanceof Composite) {
foreach ($constraint->getNestedContraints() as $nestedContraint) {
$this->checkConstraint($nestedContraint);
foreach ($constraint->getNestedConstraints() as $nestedConstraint) {
$this->checkConstraint($nestedConstraint);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Mapping/MemberMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ private function checkConstraint(Constraint $constraint)
}

if ($constraint instanceof Composite) {
foreach ($constraint->getNestedContraints() as $< 10000 span class="x x-first x-last">nestedContraint) {
$this->checkConstraint($nestedContraint);
foreach ($constraint->getNestedConstraints() as $nestedConstraint) {
$this->checkConstraint($nestedConstraint);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarExporter/Internal/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private static function exportRegistry(Registry $value, string $indent, string $
$r = '\\'.Registry::class;
$j = -1;

foreach ($value as $k => $class) {
foreach ($value->classes as $k => $class) {
if (':' === ($class[1] ?? null)) {
$serializables[$k] = $class;
continue;
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/VarExporter/Internal/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class Registry
public static $cloneable = [];
public static $instantiableWithoutConstructor = [];

public $classes = [];

public function __construct(array $classes)
{
foreach ($classes as $i => $class) {
$this->$i = $class;
}
$this->classes = $classes;
}

public static function unserialize($objects, $serializables)
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/VarExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ public function __construct()

class Php74Serializable implements \Serializable
{
public $foo;

public function __serialize(): array
{
return [$this->foo = new \stdClass()];
Expand Down
0