11<?php
22
3+ /*
4+ * This file is part of the zenstruck/foundry package.
5+ *
6+ * (c) Kevin Bond <kevinbond@gmail.com>
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
312namespace Zenstruck \Foundry \Test \Behat ;
413
514use Behat \Behat \Definition \Call \DefinitionCall ;
1423/**
1524 * @internal
1625 *
17- * Transforms TableNodes into FoundryTableNodes where all types are resolved.
26+ * Transforms TableNodes into FoundryTableNodes where all types are resolved
1827 */
1928final class FoundryCallFilter implements CallFilter
2029{
@@ -41,28 +50,26 @@ public function filterCall(Call $call): Call
4150 if (
4251 !$ call instanceof DefinitionCall
4352 || !$ call ->getCallee ()->getReflection () instanceof \ReflectionMethod
44- || $ call ->getCallee ()->getReflection ()->class !== FoundryContext:: class
53+ || FoundryContext::class !== $ call ->getCallee ()->getReflection ()->class
4554 ) {
4655 return $ call ;
4756 }
4857
4958 $ arguments = $ call ->getArguments ();
5059
5160 if (!isset ($ arguments ['factoryShortName ' ])) {
52- throw new \InvalidArgumentException (
53- <<<ERROR
61+ throw new \InvalidArgumentException (<<<ERROR
5462 Cannot filter call without a " \$factoryShortName" argument.
5563 This must be the name of the argument in the #[Given], #[When], #[Then] definitions."
56- ERROR
57- );
64+ ERROR );
5865 }
59-
66+
6067 return new DefinitionCall (
6168 $ call ->getEnvironment (),
6269 $ call ->getFeature (),
6370 $ call ->getStep (),
6471 $ call ->getCallee (),
65- array_map (
72+ \ array_map (
6673 fn (mixed $ argument ) => match ($ argument instanceof TableNode) {
6774 true => $ this ->normalizeObjectParameters ($ argument , $ arguments ['factoryShortName ' ]),
6875 false => $ argument ,
@@ -77,30 +84,30 @@ private function normalizeObjectParameters(TableNode $tableNode, string $factory
7784 {
7885 $ table = $ tableNode ->getTable ();
7986
80- $ headKey = array_key_first ($ table );
81- $ thead = array_shift ($ table );
87+ $ headKey = \ array_key_first ($ table );
88+ $ thead = \ array_shift ($ table );
8289
8390 return FoundryTableNode::create (
8491 $ this ->factoryResolver ,
8592 $ this ->objectRegistry ,
86- ( \Closure::bind (
87- fn () => $ this ->maxLineLength ,
93+ \Closure::bind (
94+ fn () => $ this ->maxLineLength ,
8895 $ tableNode ,
8996 TableNode::class
90- )()) ,
97+ )(),
9198 [ // @phpstan-ignore argument.type (TableNode has the same problem: array $table is not really lists)
9299 $ headKey => $ thead , // @phpstan-ignore array.invalidKey
93- ...array_map (
94- function (array $ parameters ) use ($ thead , $ factoryShortName ): array {
100+ ...\ array_map (
101+ function (array $ parameters ) use ($ thead , $ factoryShortName ): array {
95102 $ normalized = [];
96103 foreach ($ parameters as $ key => $ value ) {
97104 if (!isset ($ thead [$ key ])) {
98- throw new \LogicException ("Table has no column for parameter \"$ key \". This should never happen, table integrity is checked in TableNode. " );
105+ throw new \LogicException ("Table has no column for parameter \"{ $ key} \". This should never happen, table integrity is checked in TableNode. " );
99106 }
100107
101108 $ propertyName = $ thead [$ key ];
102109
103- if ($ propertyName === ' _ref ' ) {
110+ if (' _ref ' === $ propertyName ) {
104111 $ normalized ['_ref ' ] = $ value ;
105112
106113 continue ;
@@ -124,7 +131,7 @@ function (array $parameters) use ($thead, $factoryShortName): array {
124131 continue ;
125132 }
126133
127- if (preg_match ('/^<ref\((?<factoryShortName>[^,]+), (?<objectName>[^)]+)\)>$/ ' , $ value , $ matches )) {
134+ if (\ preg_match ('/^<ref\((?<factoryShortName>[^,]+), (?<objectName>[^)]+)\)>$/ ' , $ value , $ matches )) {
128135 try {
129136 $ normalized [$ propertyName ] = $ this ->objectRegistry ->getByFactoryShortName ($ matches ['factoryShortName ' ], $ matches ['objectName ' ]);
130137 } catch (ObjectNotFound $ e ) {
@@ -153,7 +160,7 @@ function (array $parameters) use ($thead, $factoryShortName): array {
153160 continue ;
154161 }
155162
156- if (is_a ($ expectedTypeClass , \DateTimeInterface::class, allow_string: true )) {
163+ if (\ is_a ($ expectedTypeClass , \DateTimeInterface::class, allow_string: true )) {
157164 try {
158165 $ normalized [$ propertyName ] = new $ expectedTypeClass ($ value );
159166
@@ -163,15 +170,15 @@ function (array $parameters) use ($thead, $factoryShortName): array {
163170 }
164171 }
165172
166- if (is_a ($ expectedTypeClass , \BackedEnum::class, allow_string: true )) {
167- $ value = is_numeric ($ value ) ? (int )$ value : $ value ;
173+ if (\ is_a ($ expectedTypeClass , \BackedEnum::class, allow_string: true )) {
174+ $ value = \ is_numeric ($ value ) ? (int ) $ value : $ value ;
168175
169- $ normalized [$ propertyName ] = $ expectedTypeClass ::tryFrom ($ value ) ?? throw InvalidObjectParameter::invalidEnumValue ($ propertyName , (string )$ value );
176+ $ normalized [$ propertyName ] = $ expectedTypeClass ::tryFrom ($ value ) ?? throw InvalidObjectParameter::invalidEnumValue ($ propertyName , (string ) $ value );
170177
171178 continue ;
172179 }
173180
174- throw new \LogicException ("Cannot normalize parameter \"$ propertyName \" with value \"$ value \". " );
181+ throw new \LogicException ("Cannot normalize parameter \"{ $ propertyName} \" with value \"{ $ value} \". " );
175182 }
176183
177184 return $ normalized ;
@@ -201,7 +208,7 @@ private function getPropertyTypeIfClass(\ReflectionClass $class, string $propert
201208 !isset ($ property )
202209 || !($ type = $ property ->getType ()) instanceof \ReflectionNamedType
203210 || $ type ->isBuiltin ()
204- || !class_exists ($ type ->getName ())
211+ || !\ class_exists ($ type ->getName ())
205212 ) {
206213 return null ;
207214 }
0 commit comments