/**
  * Finds step definition, that match specified step.
  *
  * @param   Behat\Gherkin\Node\StepNode     $step   found step
  *
  * @return  Behat\Behat\Definition\Definition
  *
  * @uses    loadDefinitions()
  *
  * @throws  Behat\Behat\Exception\Ambiguous  if step description is ambiguous
  * @throws  Behat\Behat\Exception\Undefined  if step definition not found
  */
 public function findDefinition(StepNode $step)
 {
     if (!count($this->definitions)) {
         $this->loadDefinitions();
     }
     $text = $step->getText();
     $multiline = $step->getArguments();
     $matches = array();
     // find step to match
     foreach ($this->definitions as $origRegex => $definition) {
         $transRegex = $this->translateDefinitionRegex($origRegex, $step->getLanguage());
         if (preg_match($origRegex, $text, $arguments) || $origRegex !== $transRegex && preg_match($transRegex, $text, $arguments)) {
             // prepare callback arguments
             $arguments = $this->prepareCallbackArguments($definition->getCallbackReflection(), array_slice($arguments, 1), $multiline);
             // transform arguments
             foreach ($arguments as $num => $argument) {
                 foreach ($this->transformations as $transformation) {
                     if ($newArgument = $transformation->transform($argument)) {
                         $arguments[$num] = $newArgument;
                     }
                 }
             }
             // set matched definition
             $definition->setMatchedText($text);
             $definition->setValues($arguments);
             $matches[] = $definition;
         }
     }
     if (count($matches) > 1) {
         throw new Ambiguous($text, $matches);
     }
     if (0 === count($matches)) {
         throw new Undefined($text);
     }
     return $matches[0];
 }
 /**
  * Finds step definition, that match specified step.
  *
  * @param ContextInterface $context
  * @param StepNode         $step
  * @param bool             $skip
  *
  * @return Definition
  *
  * @uses loadDefinitions()
  *
  * @throws AmbiguousException if step description is ambiguous
  * @throws UndefinedException if step definition not found
  */
 public function findDefinition(ContextInterface $context, StepNode $step, $skip = false)
 {
     $text = $step->getText();
     $multiline = $step->getArguments();
     $matches = array();
     // find step to match
     foreach ($this->getDefinitions() as $origRegex => $definition) {
         $transRegex = $this->translateDefinitionRegex($origRegex, $step->getLanguage());
         // if not regex really (string) - transform into it
         if (0 !== strpos($origRegex, '/')) {
             $origRegex = '/^' . preg_quote($origRegex, '/') . '$/';
             $transRegex = '/^' . preg_quote($transRegex, '/') . '$/';
         }
         if (preg_match($origRegex, $text, $arguments) || $origRegex !== $transRegex && preg_match($transRegex, $text, $arguments)) {
             // prepare callback arguments
             $arguments = $this->prepareCallbackArguments($context, $definition->getCallbackReflection(), array_slice($arguments, 1), $multiline);
             if (!$skip) {
                 // transform arguments
                 foreach ($arguments as &$argument) {
                     foreach ($this->getTransformations() as $trans) {
                         $transRegex = $this->translateDefinitionRegex($trans->getRegex(), $step->getLanguage());
                         $newArgument = $trans->transform($transRegex, $context, $argument);
                         if (null !== $newArgument) {
                             $argument = $newArgument;
                         }
                     }
                 }
             }
             // set matched definition
             $definition->setMatchedText($text);
             $definition->setValues($arguments);
             $matches[] = $definition;
         }
     }
     if (count($matches) > 1) {
         throw new AmbiguousException($text, $matches);
     }
     if (0 === count($matches)) {
         throw new UndefinedException($text);
     }
     return $matches[0];
 }