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

Skip to content

Commit 87fa44e

Browse files
committed
Change constructor parameter order
1 parent 2a8e162 commit 87fa44e

7 files changed

Lines changed: 141 additions & 40 deletions

File tree

UPGRADE-3.x.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
UPGRADE 3.x
22
===========
33

4-
UPGRADE FROM 3.81 to 3.xx
4+
UPGRADE FROM 3.xx to 3.xx
55
=========================
66

77
### Sonata\AdminBundle\Admin\Pool
@@ -18,13 +18,14 @@ Not passing a `Symfony\Component\PropertyAccess\PropertyAccessorInterface` insta
1818

1919
### Sonata\AdminBundle\Admin\AdminHelper
2020

21-
Not passing a `Symfony\Component\PropertyAccess\PropertyAccessorInterface` instance as argument 2 instantiating
21+
Not passing a `Symfony\Component\PropertyAccess\PropertyAccessorInterface` instance as argument 1 instantiating
2222
`Sonata\AdminBundle\Admin\AdminHelper` is deprecated.
2323

2424
### Sonata\AdminBundle\Twig\Extension\SonataAdminExtension
2525

26-
Not passing a `Symfony\Component\PropertyAccess\PropertyAccessorInterface` instance as argument 6 instantiating
27-
`Sonata\AdminBundle\Admin\SonataAdminExtension` is deprecated.
26+
Argument 5 of `Sonata\AdminBundle\Admin\SonataAdminExtension` constructor SHOULD be a
27+
`Symfony\Component\PropertyAccess\PropertyAccessorInterface` instance and argument 6 SHOULD be a
28+
`Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface` instance or "null".
2829

2930
UPGRADE FROM 3.80 to 3.81
3031
=========================

src/Admin/AdminHelper.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,51 @@ class AdminHelper
5252
private $propertyAccessor;
5353

5454
/**
55-
* NEXT_MAJOR: Make $propertyAccessor mandatory and remove $pool.
55+
* NEXT_MAJOR: Change signature for (PropertyAccessorInterface $propertyAccessor).
5656
*/
57-
public function __construct(Pool $pool, ?PropertyAccessorInterface $propertyAccessor = null)
57+
public function __construct($poolOrPropertyAccessor, ?Pool $pool = null)
5858
{
59-
$this->pool = $pool;
59+
// NEXT_MAJOR: Remove this block.
60+
if (!$poolOrPropertyAccessor instanceof Pool && !$poolOrPropertyAccessor instanceof PropertyAccessorInterface) {
61+
throw new \TypeError(sprintf(
62+
'Argument 1 passed to "%s()" must be either an instance of %s or %s, %s given.',
63+
__METHOD__,
64+
Pool::class,
65+
PropertyAccessorInterface::class,
66+
\is_object($poolOrPropertyAccessor) ? \get_class($poolOrPropertyAccessor) : \gettype($poolOrPropertyAccessor)
67+
));
68+
}
6069

6170
// NEXT_MAJOR: Remove this block.
62-
if (null === $propertyAccessor) {
71+
if ($poolOrPropertyAccessor instanceof Pool) {
6372
@trigger_error(sprintf(
64-
'Omitting the argument 2 for "%s()" or passing "null" is deprecated since sonata-project/admin-bundle'
65-
.' 3.x and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
73+
'Passing an instance of "%s" as the argument 1 for "%s()" is deprecated since'
74+
.' sonata-project/admin-bundle 3.x and will throw a \TypeError in version 4.0.'
75+
.' You MUST pass an instance of %s instead.',
76+
Pool::class,
6677
__METHOD__,
6778
PropertyAccessorInterface::class
6879
), E_USER_DEPRECATED);
6980

70-
$propertyAccessor = $pool->getPropertyAccessor();
81+
$this->pool = $poolOrPropertyAccessor;
82+
$this->propertyAccessor = $poolOrPropertyAccessor->getPropertyAccessor();
83+
84+
return;
7185
}
7286

73-
$this->propertyAccessor = $propertyAccessor;
87+
// NEXT_MAJOR: Remove this block.
88+
if (null === $pool) {
89+
throw new \TypeError(sprintf(
90+
'Argument 2 passed to "%s()" must be an instance of "%s", "null" given.',
91+
__METHOD__,
92+
Pool::class
93+
));
94+
}
95+
96+
// NEXT_MAJOR: Change $poolOrPropertyAccessor to $propertyAccessor.
97+
$this->propertyAccessor = $poolOrPropertyAccessor;
98+
// NEXT_MAJOR: Remove next line.
99+
$this->pool = $pool;
74100
}
75101

76102
/**

src/Resources/config/twig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
(new ReferenceConfigurator('logger'))->nullOnInvalid(),
5252
new ReferenceConfigurator('translator'),
5353
new ReferenceConfigurator('service_container'),
54-
new ReferenceConfigurator('security.authorization_checker'),
5554
new ReferenceConfigurator('property_accessor'),
55+
new ReferenceConfigurator('security.authorization_checker'),
5656
])
5757
->call('setXEditableTypeMapping', [
5858
'%sonata.admin.twig.extension.x_editable_type_mapping%',

src/Twig/Extension/SonataAdminExtension.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public function __construct(
9595
?LoggerInterface $logger = null,
9696
$translator = null,
9797
?ContainerInterface $templateRegistries = null,
98-
?AuthorizationCheckerInterface $securityChecker = null,
99-
?PropertyAccessorInterface $propertyAccessor = null
98+
$propertyAccessorOrSecurityChecker = null,
99+
?AuthorizationCheckerInterface $securityChecker = null
100100
) {
101101
// NEXT_MAJOR: make the translator parameter required, move TranslatorInterface check to method signature
102102
// and remove this block
@@ -125,23 +125,51 @@ public function __construct(
125125
}
126126

127127
// NEXT_MAJOR: Remove this block.
128-
if (null === $propertyAccessor) {
128+
if (!$propertyAccessorOrSecurityChecker instanceof PropertyAccessorInterface
129+
&& !$propertyAccessorOrSecurityChecker instanceof AuthorizationCheckerInterface
130+
&& null !== $propertyAccessorOrSecurityChecker
131+
) {
132+
throw new \TypeError(sprintf(
133+
'Argument 5 must be an instance of "%s" or "%s" or null, "%s given"',
134+
PropertyAccessorInterface::class,
135+
AuthorizationCheckerInterface::class,
136+
\is_object($propertyAccessorOrSecurityChecker) ? \get_class($propertyAccessorOrSecurityChecker) : \gettype($propertyAccessorOrSecurityChecker)
137+
));
138+
}
139+
140+
// NEXT_MAJOR: Remove this block and extract the else part outside.
141+
if ($propertyAccessorOrSecurityChecker instanceof AuthorizationCheckerInterface) {
142+
@trigger_error(sprintf(
143+
'Passing an instance of "%s" the argument 5 for "%s()" is deprecated since sonata-project/admin-bundle'
144+
.' 3.x and will throw a \TypeError in version 4.0. You MUST pass an instance of "%s" instead and pass'
145+
.' the instance of "%s" as 6 argument.',
146+
AuthorizationCheckerInterface::class,
147+
__METHOD__,
148+
PropertyAccessorInterface::class,
149+
AuthorizationCheckerInterface::class
150+
), E_USER_DEPRECATED);
151+
152+
$this->securityChecker = $propertyAccessorOrSecurityChecker;
153+
$this->propertyAccessor = $pool->getPropertyAccessor();
154+
} elseif (null === $propertyAccessorOrSecurityChecker) {
129155
@trigger_error(sprintf(
130-
'Omitting the argument 6 for "%s()" or passing "null" is deprecated since sonata-project/admin-bundle'
156+
'Omitting the argument 5 for "%s()" or passing "null" is deprecated since sonata-project/admin-bundle'
131157
.' 3.x and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
132158
__METHOD__,
133159
PropertyAccessorInterface::class
134160
), E_USER_DEPRECATED);
135161

136-
$propertyAccessor = $pool->getPropertyAccessor();
162+
$this->propertyAccessor = $pool->getPropertyAccessor();
163+
$this->securityChecker = $securityChecker;
164+
} else {
165+
$this->securityChecker = $securityChecker;
166+
$this->propertyAccessor = $propertyAccessorOrSecurityChecker;
137167
}
138168

139169
$this->pool = $pool;
140170
$this->logger = $logger;
141171
$this->translator = $translator;
142172
$this->templateRegistries = $templateRegistries;
143-
$this->securityChecker = $securityChecker;
144-
$this->propertyAccessor = $propertyAccessor;
145173
}
146174

147175
/**

tests/Action/SetObjectFieldValueActionTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public function testSetObjectFieldValueAction(): void
132132
null,
133133
$translator,
134134
$container,
135-
null,
136-
$this->propertyAccessor
135+
$this->propertyAccessor,
136+
null
137137
));
138138
$fieldDescription->method('getOption')->willReturnMap([
139139
['editable', null, true],
@@ -199,8 +199,8 @@ public function testSetObjectFieldValueActionWithDate($timezone, \DateTimeZone $
199199
null,
200200
$translator,
201201
$container,
202-
null,
203-
$this->propertyAccessor
202+
$this->propertyAccessor,
203+
null
204204
));
205205
$fieldDescription->method('getOption')->willReturnMap([
206206
['timezone', null, $timezone],
@@ -261,8 +261,8 @@ public function testSetObjectFieldValueActionOnARelationField(): void
261261
null,
262262
$translator,
263263
$container,
264-
null,
265-
$this->propertyAccessor
264+
$this->propertyAccessor,
265+
null
266266
));
267267
$fieldDescription->method('getType')->willReturn('choice');
268268
$fieldDescription->method('getOption')->willReturnMap([
@@ -349,8 +349,8 @@ public function testSetObjectFieldEditableMultipleValue(): void
349349
null,
350350
$translator,
351351
$container,
352-
null,
353-
$this->propertyAccessor
352+
$this->propertyAccessor,
353+
null
354354
));
355355
$fieldDescription->method('getOption')->willReturnMap([
356356
['data_transformer', null, null],
@@ -407,8 +407,8 @@ public function testSetObjectFieldTransformed(): void
407407
null,
408408
$translator,
409409
$container,
410-
null,
411-
$this->propertyAccessor
410+
$this->propertyAccessor,
411+
null
412412
));
413413
$fieldDescription->method('getOption')->willReturnMap([
414414
['data_transformer', null, $dataTransformer],
@@ -467,8 +467,8 @@ public function testSetObjectFieldOverrideTransformer(): void
467467
null,
468468
$translator,
469469
$container,
470-
null,
471-
$this->propertyAccessor
470+
$this->propertyAccessor,
471+
null
472472
));
473473
$fieldDescription->method('getOption')->willReturnMap([
474474
['data_transformer', null, $dataTransformer],

tests/Admin/AdminHelperTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function setUp(): void
5454

5555
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
5656
$pool = new Pool($container, 'title', 'logo.png');
57-
$this->helper = new AdminHelper($pool, $this->propertyAccessor);
57+
$this->helper = new AdminHelper($this->propertyAccessor, $pool);
5858
}
5959

6060
/**
@@ -67,9 +67,10 @@ public function testDeprecatedConstructingWithoutPropertyAccessor(): void
6767
$pool = new Pool(new Container(), 'title', 'logo.png');
6868

6969
$this->expectDeprecation(sprintf(
70-
'Omitting the argument 2 for "%s::__construct()" or passing "null" is deprecated since'
71-
.' sonata-project/admin-bundle 3.x and will throw a \TypeError in version 4.0. You must pass an instance'
70+
'Passing an instance of "%s" as the argument 1 for "%s::__construct()" is deprecated since'
71+
.' sonata-project/admin-bundle 3.x and will throw a \TypeError in version 4.0. You MUST pass an instance'
7272
.' of %s instead.',
73+
Pool::class,
7374
AdminHelper::class,
7475
PropertyAccessorInterface::class
7576
));
@@ -223,7 +224,7 @@ public function testAppendFormFieldElement(): void
223224
$container = new Container();
224225

225226
$pool = new Pool($container, 'title', 'logo.png');
226-
$helper = new AdminHelper($pool, $this->propertyAccessor);
227+
$helper = new AdminHelper($this->propertyAccessor, $pool);
227228

228229
$admin = $this->createMock(AdminInterface::class);
229230
$admin

tests/Twig/Extension/SonataAdminExtensionTest.php

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
2525
use Sonata\AdminBundle\Tests\Fixtures\StubFilesystemLoader;
2626
use Sonata\AdminBundle\Twig\Extension\SonataAdminExtension;
27+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
2728
use Symfony\Bridge\Twig\AppVariable;
2829
use Symfony\Bridge\Twig\Extension\RoutingExtension;
2930
use Symfony\Bridge\Twig\Extension\TranslationExtension;
3031
use Symfony\Component\Config\FileLocator;
3132
use Symfony\Component\DependencyInjection\Container;
3233
use Symfony\Component\HttpFoundation\Request;
3334
use Symfony\Component\PropertyAccess\PropertyAccess;
34-
use Symfony\Component\PropertyAccess\PropertyAccessor;
35+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
3536
use Symfony\Component\Routing\Generator\UrlGenerator;
3637
use Symfony\Component\Routing\Loader\XmlFileLoader;
3738
use Symfony\Component\Routing\RequestContext;
@@ -51,6 +52,8 @@
5152
*/
5253
class SonataAdminExtensionTest extends TestCase
5354
{
55+
use ExpectDeprecationTrait;
56+
5457
/**
5558
* @var SonataAdminExtension
5659
*/
@@ -166,15 +169,15 @@ protected function setUp(): void
166169
$this->container->set('sonata_admin_foo_service.template_registry', $this->templateRegistry);
167170

168171
$this->securityChecker = $this->createStub(AuthorizationCheckerInterface::class);
169-
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
172+
$propertyAccessor = PropertyAccess::createPropertyAccessor();
170173

171174
$this->twigExtension = new SonataAdminExtension(
172175
$this->pool,
173176
$this->logger,
174177
$this->translator,
175178
$this->container,
176-
$this->securityChecker,
177-
$this->propertyAccessor
179+
$propertyAccessor,
180+
$this->securityChecker
178181
);
179182
$this->twigExtension->setXEditableTypeMapping($this->xEditableTypeMapping);
180183

@@ -265,6 +268,48 @@ protected function setUp(): void
265268
->willReturn('Data');
266269
}
267270

271+
/**
272+
* NEXT_MAJOR: Remove this method.
273+
*/
274+
public function testConstructThrowsExceptionWithWrongPropertyAccessOrAuthorizationCheckerArgument(): void
275+
{
276+
$this->expectException(\TypeError::class);
277+
278+
new SonataAdminExtension(
279+
$this->pool,
280+
null,
281+
$this->translator,
282+
$this->container,
283+
new \stdClass()
284+
);
285+
}
286+
287+
/**
288+
* NEXT_MAJOR: Remove this method.
289+
*
290+
* @group legacy
291+
*/
292+
public function testConstructTriggersDeprecationWithAuthorizationCheckerArgument(): void
293+
{
294+
$this->expectDeprecation(sprintf(
295+
'Passing an instance of "%s" the argument 5 for "%s::__construct()" is deprecated since'
296+
.' sonata-project/admin-bundle 3.x and will throw a \TypeError in version 4.0. You MUST pass an instance'
297+
.' of "%s" instead and pass the instance of "%s" as 6 argument.',
298+
AuthorizationCheckerInterface::class,
299+
SonataAdminExtension::class,
300+
PropertyAccessorInterface::class,
301+
AuthorizationCheckerInterface::class
302+
));
303+
304+
new SonataAdminExtension(
305+
$this->pool,
306+
null,
307+
$this->translator,
308+
$this->container,
309+
$this->securityChecker
310+
);
311+
}
312+
268313
/**
269314
* NEXT_MAJOR: Remove this method.
270315
*

0 commit comments

Comments
 (0)