Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / Printer / Helper / WidthCalculator.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Output\Node\Printer\Helper;
12
13 use Behat\Gherkin\Node\ExampleNode;
14 use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
15 use Behat\Gherkin\Node\StepNode;
16
17 /**
18  * Calculates width of scenario. Width of scenario = max width of scenario title and scenario step texts.
19  *
20  * @author Konstantin Kudryashov <ever.zet@gmail.com>
21  */
22 final class WidthCalculator
23 {
24     /**
25      * Calculates scenario width.
26      *
27      * @param Scenario $scenario
28      * @param integer  $indentation
29      * @param integer  $subIndentation
30      *
31      * @return integer
32      */
33     public function calculateScenarioWidth(Scenario $scenario, $indentation, $subIndentation)
34     {
35         $length = $this->calculateScenarioHeaderWidth($scenario, $indentation);
36
37         foreach ($scenario->getSteps() as $step) {
38             $stepLength = $this->calculateStepWidth($step, $indentation + $subIndentation);
39             $length = max($length, $stepLength);
40         }
41
42         return $length;
43     }
44
45     /**
46      * Calculates outline examples width.
47      *
48      * @param ExampleNode $example
49      * @param integer     $indentation
50      * @param integer     $subIndentation
51      *
52      * @return integer
53      */
54     public function calculateExampleWidth(ExampleNode $example, $indentation, $subIndentation)
55     {
56         $length = $this->calculateScenarioHeaderWidth($example, $indentation);
57
58         foreach ($example->getSteps() as $step) {
59             $stepLength = $this->calculateStepWidth($step, $indentation + $subIndentation);
60             $length = max($length, $stepLength);
61         }
62
63         return $length;
64     }
65
66     /**
67      * Calculates scenario header width.
68      *
69      * @param Scenario $scenario
70      * @param integer  $indentation
71      *
72      * @return integer
73      */
74     public function calculateScenarioHeaderWidth(Scenario $scenario, $indentation)
75     {
76         $indentText = str_repeat(' ', intval($indentation));
77
78         if ($scenario instanceof ExampleNode) {
79             $header = sprintf('%s%s', $indentText, $scenario->getTitle());
80         } else {
81             $title = $scenario->getTitle();
82             $lines = explode("\n", $title);
83             $header = sprintf('%s%s: %s', $indentText, $scenario->getKeyword(), array_shift($lines));
84         }
85
86         return mb_strlen(rtrim($header), 'utf8');
87     }
88
89     /**
90      * Calculates step width.
91      *
92      * @param StepNode $step
93      * @param integer  $indentation
94      *
95      * @return integer
96      */
97     public function calculateStepWidth(StepNode $step, $indentation)
98     {
99         $indentText = str_repeat(' ', intval($indentation));
100
101         $text = sprintf('%s%s %s', $indentText, $step->getKeyword(), $step->getText());
102
103         return mb_strlen($text, 'utf8');
104     }
105 }