Skip to content

Commit e0fd4ef

Browse files
BooleanTypeLeroid
authored andcommitted
Return type declaration in factory
1 parent 6c50bbc commit e0fd4ef

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

service_container/factories.rst

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ Usage example
308308
The following example is intended to show how to create and use a factory method in Symfony framework.
309309
Suppose you want to realize the factory method pattern for services, that describe two delivery methods - DHL and UPS.
310310

311-
Firstly, define marker interface for delivery methods classes::
311+
Services (subclasses) definition::
312312

313313
// src/Deliveries/DeliveryInterface.php
314314
namespace App\Deliveries;
@@ -317,8 +317,6 @@ Firstly, define marker interface for delivery methods classes::
317317
{
318318
}
319319

320-
Services (subclasses) definition::
321-
322320
// src/Deliveries/DHL.php
323321
namespace App\Deliveries;
324322
@@ -353,17 +351,12 @@ Factory definition::
353351
namespace App\Factories;
354352
355353
use App\Deliveries\DeliveryInterface;
356-
use RuntimeException;
357354
358355
abstract class DeliveryFactory
359356
{
360-
public static function create($deliveryMethod)
357+
public static function create(string $deliveryMethod): DeliveryInterface
361358
{
362-
$delivery = new $deliveryMethod;
363-
if ( ! $delivery instanceof DeliveryInterface) {
364-
throw new RuntimeException(sprintf('%1$s should implement %2$s.', $deliveryMethod, DeliveryInterface::class));
365-
}
366-
359+
$delivery = new $deliveryMethod;
367360
$delivery->costLabel = 'Delivery cost is: ';
368361
369362
return $delivery;

0 commit comments

Comments
 (0)