Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee334d5

Browse files
committed
Merge branch '6.4' into 7.3
* 6.4: do not use PHPUnit mock objects without configured expectations do not use PHPUnit mock objects without configured expectations do not use PHPUnit mock objects without configured expectations [PropertyAccess][PropertyInfo][Serializer] Skip methods that look like getters but return void or never [Form][TwigBridge] Prevent cached block prefixes from leaking across nested collections [Uid] Remove $uid from InvalidArgumentException message
2 parents e6769b1 + c5f0a98 commit ee334d5

7 files changed

Lines changed: 78 additions & 11 deletions

File tree

Mapping/Loader/AttributeLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
116116
}
117117

118118
$accessorOrMutator = match ($name[0]) {
119-
's' => str_starts_with($name, 'set') && isset($name[$i = 3]),
119+
's' => str_starts_with($name, 'set') && isset($name[$i = 3]) && $method->getNumberOfParameters(),
120120
'g' => str_starts_with($name, 'get') && isset($name[$i = 3]),
121121
'h' => str_starts_with($name, 'has') && isset($name[$i = 3]),
122122
'c' => str_starts_with($name, 'can') && isset($name[$i = 3]),
123123
'i' => str_starts_with($name, 'is') && isset($name[$i = 2]),
124124
default => false,
125-
};
125+
} && ('s' === $name[0] || !$method->getNumberOfRequiredParameters() && !\in_array((string) $method->getReturnType(), ['void', 'never'], true));
126+
126127
if ($accessorOrMutator && !ctype_lower($name[$i])) {
127128
if ($this->hasProperty($method->getDeclaringClass(), $name)) {
128129
$attributeName = $name;

Normalizer/GetSetMethodNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ private function isGetMethod(\ReflectionMethod $method): bool
8787
return !$method->isStatic()
8888
&& !($method->getAttributes(Ignore::class) || $method->getAttributes(LegacyIgnore::class))
8989
&& !$method->getNumberOfRequiredParameters()
90+
&& !\in_array((string) $method->getReturnType(), ['void', 'never'], true)
9091
&& ((2 < ($methodLength = \strlen($method->name)) && str_starts_with($method->name, 'is') && !ctype_lower($method->name[2]))
91-
|| (3 < $methodLength && (str_starts_with($method->name, 'has') || str_starts_with($method->name, 'get')) && !ctype_lower($method->name[3]))
92+
|| (3 < $methodLength && (str_starts_with($method->name, 'has') || str_starts_with($method->name, 'get') || str_starts_with($method->name, 'can')) && !ctype_lower($method->name[3]))
9293
);
9394
}
9495

Normalizer/ObjectNormalizer.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ protected function extractAttributes(object $object, ?string $format = null, arr
7676

7777
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
7878
if (
79-
0 !== $reflMethod->getNumberOfRequiredParameters()
79+
$reflMethod->getNumberOfRequiredParameters()
8080
|| $reflMethod->isStatic()
8181
|| $reflMethod->isConstructor()
8282
|| $reflMethod->isDestructor()
83+
|| \in_array((string) $reflMethod->getReturnType(), ['void', 'never'], true)
8384
) {
8485
continue;
8586
}
@@ -183,11 +184,7 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
183184

184185
private function hasAttributeAccessorMethod(string $class, string $attribute): bool
185186
{
186-
if (!isset(self::$reflectionCache[$class])) {
187-
self::$reflectionCache[$class] = new \ReflectionClass($class);
188-
}
189-
190-
$reflection = self::$reflectionCache[$class];
187+
$reflection = self::$reflectionCache[$class] ??= new \ReflectionClass($class);
191188

192189
if (!$reflection->hasMethod($attribute)) {
193190
return false;
@@ -197,6 +194,7 @@ private function hasAttributeAccessorMethod(string $class, string $attribute): b
197194

198195
return !$method->isStatic()
199196
&& !$method->getAttributes(Ignore::class)
200-
&& !$method->getNumberOfRequiredParameters();
197+
&& !$method->getNumberOfRequiredParameters()
198+
&& !\in_array((string) $method->getReturnType(), ['void', 'never'], true);
201199
}
202200
}

Tests/Fixtures/Attributes/AccessorishGetters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function hasField3()
3333
{
3434
}
3535

36-
public function setField4()
36+
public function setField4($value)
3737
{
3838
}
3939
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
class VoidNeverReturnTypeDummy
15+
{
16+
public string $normalProperty = 'value';
17+
18+
public function getNormalProperty(): string
19+
{
20+
return $this->normalProperty;
21+
}
22+
23+
public function getVoidProperty(): void
24+
{
25+
// This looks like a getter but returns void, should be ignored
26+
}
27+
28+
public function getNeverProperty(): never
29+
{
30+
// This looks like a getter but returns never, should be ignored
31+
throw new \Exception('Never returns');
32+
}
33+
34+
public function setValue(): void
35+
{
36+
// This looks like a setter but has no parameters, should be ignored as accessor
37+
}
38+
39+
public function setNeverValue(): never
40+
{
41+
// This looks like a setter but has no parameters and returns never, should be ignored as accessor
42+
throw new \Exception('Never returns');
43+
}
44+
}
45+

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\Serializer\Tests\Fixtures\ScalarNormalizer;
3737
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
3838
use Symfony\Component\Serializer\Tests\Fixtures\StdClassNormalizer;
39+
use Symfony\Component\Serializer\Tests\Fixtures\VoidNeverReturnTypeDummy;
3940
use Symfony\Component\Serializer\Tests\Normalizer\Features\CacheableObjectAttributesTestTrait;
4041
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait;
4142
use Symfony\Component\Serializer\Tests\Normalizer\Features\CircularReferenceTestTrait;
@@ -626,6 +627,16 @@ public function testDiscriminatorWithAllowExtraAttributesFalse()
626627

627628
$this->assertInstanceOf(GetSetMethodDiscriminatedDummyOne::class, $obj);
628629
}
630+
631+
public function testSkipVoidNeverReturnTypeAccessors()
632+
{
633+
$obj = new VoidNeverReturnTypeDummy();
634+
$normalized = $this->normalizer->normalize($obj);
635+
$this->assertArrayHasKey('normalProperty', $normalized);
636+
$this->assertArrayNotHasKey('voidProperty', $normalized);
637+
$this->assertArrayNotHasKey('neverProperty', $normalized);
638+
$this->assertEquals('value', $normalized['normalProperty']);
639+
}
629640
}
630641

631642
class GetSetDummy

Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use Symfony\Component\Serializer\Tests\Fixtures\Php80Dummy;
5151
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
5252
use Symfony\Component\Serializer\Tests\Fixtures\StdClassNormalizer;
53+
use Symfony\Component\Serializer\Tests\Fixtures\VoidNeverReturnTypeDummy;
5354
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
5455
use Symfony\Component\Serializer\Tests\Normalizer\Features\CacheableObjectAttributesTestTrait;
5556
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait;
@@ -1212,6 +1213,16 @@ public function testNormalizeObjectWithGroupsAndIsPrefixedProperty()
12121213
$normalizedWithGroups = $normalizer->normalize($object, null, [AbstractNormalizer::GROUPS => ['test']]);
12131214
$this->assertArrayHasKey('isSomething', $normalizedWithGroups);
12141215
}
1216+
1217+
public function testSkipVoidNeverReturnTypeAccessors()
1218+
{
1219+
$obj = new VoidNeverReturnTypeDummy();
1220+
$normalized = $this->normalizer->normalize($obj);
1221+
$this->assertArrayHasKey('normalProperty', $normalized);
1222+
$this->assertArrayNotHasKey('voidProperty', $normalized);
1223+
$this->assertArrayNotHasKey('neverProperty', $normalized);
1224+
$this->assertEquals('value', $normalized['normalProperty']);
1225+
}
12151226
}
12161227

12171228
class ProxyObjectDummy extends ObjectDummy

0 commit comments

Comments
 (0)