Code Duplication    Length = 63-64 lines in 2 locations

src/Behat/Gherkin/Parser.php 2 locations

@@ 298-360 (lines=63) @@
295
     *
296
     * @throws ParserException
297
     */
298
    protected function parseBackground()
299
    {
300
        $token = $this->expectTokenType('Background');
301
302
        $title = trim($token['value']);
303
        $keyword = $token['keyword'];
304
        $line = $token['line'];
305
        $example = null;
306
307
        if (count($this->popTags())) {
308
            throw new ParserException(sprintf(
309
                'Background can not be tagged, but it is on line: %d%s',
310
                $line,
311
                $this->file ? ' in file: ' . $this->file : ''
312
            ));
313
        }
314
315
        // Parse description and steps
316
        $steps = array();
317
        $allowedTokenTypes = array('Step', 'Newline', 'Text', 'Comment', 'Examples');
318
        while (in_array($this->predictTokenType(), $allowedTokenTypes)) {
319
            $node = $this->parseExpression();
320
321
            if ($node instanceof ExampleTableNode) {
322
                $example = $node;
323
                continue;
324
            }
325
326
            if ($node instanceof StepNode) {
327
                $steps[] = $this->normalizeStepNodeKeywordType($node, $steps);
328
                continue;
329
            }
330
331
            if (!count($steps) && is_string($node)) {
332
                $text = preg_replace('/^\s{0,' . ($token['indent'] + 2) . '}|\s*$/', '', $node);
333
                $title .= "\n" . $text;
334
                continue;
335
            }
336
337
            if ("\n" === $node) {
338
                continue;
339
            }
340
341
            if (is_string($node)) {
342
                throw new ParserException(sprintf(
343
                    'Expected Step, but got text: "%s"%s',
344
                    $node,
345
                    $this->file ? ' in file: ' . $this->file : ''
346
                ));
347
            }
348
349
            if (!$node instanceof StepNode) {
350
                throw new ParserException(sprintf(
351
                    'Expected Step, but got %s on line: %d%s',
352
                    $node->getNodeType(),
353
                    $node->getLine(),
354
                    $this->file ? ' in file: ' . $this->file : ''
355
                ));
356
            }
357
        }
358
359
        return new BackgroundNode(rtrim($title) ?: null, $steps, $keyword, $line, $example);
360
    }
361
362
    /**
363
     * Parses scenario token & returns it's node.
@@ 426-489 (lines=64) @@
423
     *
424
     * @throws ParserException
425
     */
426
    protected function parseOutline()
427
    {
428
        $token = $this->expectTokenType('Outline');
429
430
        $title = trim($token['value']);
431
        $tags = $this->popTags();
432
        $keyword = $token['keyword'];
433
        $examples = null;
434
        $line = $token['line'];
435
436
        // Parse description, steps and examples
437
        $steps = array();
438
        while (in_array($this->predictTokenType(), array('Step', 'Examples', 'Newline', 'Text', 'Comment'))) {
439
            $node = $this->parseExpression();
440
441
            if ($node instanceof StepNode) {
442
                $steps[] = $this->normalizeStepNodeKeywordType($node, $steps);
443
                continue;
444
            }
445
446
            if ($node instanceof ExampleTableNode) {
447
                $examples = $node;
448
                continue;
449
            }
450
451
            if (!count($steps) && is_string($node)) {
452
                $text = preg_replace('/^\s{0,' . ($token['indent'] + 2) . '}|\s*$/', '', $node);
453
                $title .= "\n" . $text;
454
                continue;
455
            }
456
457
            if ("\n" === $node) {
458
                continue;
459
            }
460
461
            if (is_string($node)) {
462
                throw new ParserException(sprintf(
463
                    'Expected Step or Examples table, but got text: "%s"%s',
464
                    $node,
465
                    $this->file ? ' in file: ' . $this->file : ''
466
                ));
467
            }
468
469
            if (!$node instanceof StepNode) {
470
                throw new ParserException(sprintf(
471
                    'Expected Step or Examples table, but got %s on line: %d%s',
472
                    $node->getNodeType(),
473
                    $node->getLine(),
474
                    $this->file ? ' in file: ' . $this->file : ''
475
                ));
476
            }
477
        }
478
479
        if (null === $examples) {
480
            throw new ParserException(sprintf(
481
                'Outline should have examples table, but got none for outline "%s" on line: %d%s',
482
                rtrim($title),
483
                $line,
484
                $this->file ? ' in file: ' . $this->file : ''
485
            ));
486
        }
487
488
        return new OutlineNode(rtrim($title) ?: null, $tags, $steps, $examples, $keyword, $line);
489
    }
490
491
    /**
492
     * Parses step token & returns it's node.