8000 feature #33497 [Contracts] Add parameter type declarations to contrac… · derrabus/symfony@65c2a11 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65c2a11

Browse files
feature symfony#33497 [Contracts] Add parameter type declarations to contracts (derrabus)
This PR was merged into the 5.0-dev branch. Discussion ---------- [Contracts] Add parameter type declarations to contracts | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#32179 | License | MIT | Doc PR | N/A This PR proposes to create a php 7.2 version of the contracts that maintains BC with Symfony 4. The PR suggests to bump the contracts version to ~~1.2~~ 2.0 on the master branch. We would still be able to maintain the contracts 1.1 branch on Symfony's 4.4 branch, should we need to patch the current contracts in the future. This move would allow us to add parameter type declarations to existing contracts interfaces and make use of them in Symfony 5. Especially the Translation and EventDispatcher components benefit a lot from this bump, imho. Contracts that will be added on the road to Symfony 6 wouldn't be restricted to the capabilities of php 7.1, which would be another benefit in my opinion. ~~<sup>1</sup> Test currently fail because the translator is called with `null` as translation key. That possibility should be deprecated imho.~~ Commits ------- bf515e1 Add parameter type declarations to contracts.
2 parents 69cd750 + bf515e1 commit 65c2a11

27 files changed

+98
-174
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
"twig/twig": "^2.10|^3.0",
2424
"psr/cache": "~1.0",
2525
"psr/container": "^1.0",
26+
"psr/event-dispatcher": "^1.0",
2627
"psr/link": "^1.0",
2728
"psr/log": "~1.0",
28-
"symfony/contracts": "^1.1.7|^2",
29+
"symfony/contracts": "^2",
2930
"symfony/polyfill-ctype": "~1.8",
3031
"symfony/polyfill-intl-grapheme": "~1.0",
3132
"symfony/polyfill-intl-icu": "~1.0",

src/Symfony/Bridge/Twig/Tests/Extension/Fixtures/StubTranslator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class StubTranslator implements TranslatorInterface
1717
{
18-
public function trans($id, array $parameters = [], $domain = null, $locale = null): string
18+
public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
1919
{
2020
return '[trans]'.strtr($id, $parameters).'[/trans]';
2121
}

src/Symfony/Bridge/Twig/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.2.9",
20-
"symfony/translation-contracts": "^1.1|^2",
20+
"symfony/translation-contracts": "^2",
2121
"twig/twig": "^2.10|^3.0"
2222
},
2323
"require-dev": {

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/DataCollectorTranslatorPassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function getNotImplementingTranslatorBagInterfaceTranslatorClassNames()
108108

109109
class TranslatorWithTranslatorBag implements TranslatorInterface
110110
{
111-
public function trans($id, array $parameters = [], $domain = null, $locale = null): string
111+
public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
112112
{
113113
}
114114
}

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpFoundation\RequestStack;
2020
use Symfony\Component\Stopwatch\Stopwatch;
21-
use Symfony\Contracts\EventDispatcher\Event;
2221
use Symfony\Contracts\Service\ResetInterface;
2322

2423
/**
@@ -129,12 +128,8 @@ public function hasListeners(string $eventName = null)
129128
/**
130129
* {@inheritdoc}
131130
*/
132-
public function dispatch($event, string $eventName = null): object
131+
public function dispatch(object $event, string $eventName = null): object
133132
{
134-
if (!\is_object($event)) {
135-
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.', EventDispatcherInterface::class, \gettype($event)));
136-
}
137-
138133
$eventName = $eventName ?? \get_class($event);
139134

140135
if (null === $this->callStack) {
@@ -143,7 +138,7 @@ public function dispatch($event, string $eventName = null): object
143138

144139
$currentRequestHash = $this->currentRequestHash = $this->requestStack && ($request = $this->requestStack->getCurrentRequest()) ? spl_object_hash($request) : '';
145140

146-
if (null !== $this->logger && ($event instanceof Event || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
141+
if (null !== $this->logger && $event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
147142
$this->logger->debug(sprintf('The "%s" event is already stopped. No listeners have been called.', $eventName));
148143
}
149144

src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1616
use Symfony\Component\Stopwatch\Stopwatch;
1717
use Symfony\Component\VarDumper\Caster\ClassStub;
18-
use Symfony\Contracts\EventDispatcher\Event;
1918

2019
/**
2120
* @author Fabien Potencier <fabien@symfony.com>
@@ -121,7 +120,7 @@ public function __invoke(object $event, string $eventName, EventDispatcherInterf
121120
$e->stop();
122121
}
123122

124-
if (($event instanceof Event || $event instanceof StoppableEventInterface) && $event->isPropagationStopped()) {
123+
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
125124
$this->stoppedPropagation = true;
126125
}
127126
}

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\EventDispatcher\StoppableEventInterface;
1515
use Symfony\Component\EventDispatcher\Debug\WrappedListener;
16-
use Symfony\Contracts\EventDispatcher\Event;
1716

1817
/**
1918
* The EventDispatcherInterface is the central point of Symfony's event listener system.
@@ -46,12 +45,8 @@ public function __construct()
4645
/**
4746
* {@inheritdoc}
4847
*/
49-
public function dispatch($event, string $eventName = null): object
48+
public function dispatch(object $event, string $eventName = null): object
5049
{
51-
if (!\is_object($event)) {
52-
throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.', EventDispatcherInterface::class, \gettype($event)));
53-
}
54-
5550
$eventName = $eventName ?? \get_class($event);
5651

5752
if (null !== $this->optimized && null !== $eventName) {
@@ -226,7 +221,7 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
226221
*/
227222
protected function callListeners(iterable $listeners, string $eventName, object $event)
228223
{
229-
$stoppable = $event instanceof Event || $event instanceof StoppableEventInterface;
224+
$stoppable = $event instanceof StoppableEventInterface;
230225

231226
foreach ($listeners as $listener) {
232227
if ($stoppable && $event->isPropagationStopped()) {

src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface EventDispatcherInterface extends ContractsEventDispatcherInterface
2525
/**
2626
* {@inheritdoc}
2727
*/
28-
public function dispatch($event, string $eventName = null): object;
28+
public function dispatch(object $event, string $eventName = null): object;
2929

3030
/**
3131
* Adds an event listener that listens on the specified events.

src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(EventDispatcherInterface $dispatcher)
2828
/**
2929
* {@inheritdoc}
3030
*/
31-
public function dispatch($event, string $eventName = null): object
31+
public function dispatch(object $event, string $eventName = null): object
3232
{
3333
return $this->dispatcher->dispatch($event, $eventName);
3434
}

src/Symfony/Component/EventDispatcher/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.2.9",
20-
"symfony/event-dispatcher-contracts": "^1.1|^2"
20+
"symfony/event-dispatcher-contracts": "^2"
2121
},
2222
"require-dev": {
2323
"symfony/dependency-injection": "^4.4|^5.0",
@@ -33,7 +33,7 @@
3333
},
3434
"provide": {
3535
"psr/event-dispatcher-implementation": "1.0",
36-
"symfony/event-dispatcher-implementation": "1.1"
36+
"symfony/event-dispatcher-implementation": "2.0"
3737
},
3838
"suggest": {
3939
"symfony/dependency-injection": "",

0 commit comments

Comments
 (0)
0