Conditions | 7 |
Paths | 10 |
Total Lines | 81 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
19 | public function outputResults(AnalysisResults $analysisResults): string |
||
20 | { |
||
21 | if (0 === $analysisResults->getCount()) { |
||
22 | return <<<XML |
||
23 | <?xml version="1.0" encoding="UTF-8"?> |
||
24 | <testsuites |
||
25 | name="SARB" tests="1" failures="0"> |
||
26 | <testsuite errors="0" tests="1" failures="0" name="Success"> |
||
27 | <testcase name="Success"/> |
||
28 | </testsuite> |
||
29 | </testsuites> |
||
30 | |||
31 | XML; |
||
32 | } |
||
33 | |||
34 | $xml = $this->getXmlString(); |
||
35 | $test = new SimpleXMLElement($xml); |
||
36 | |||
37 | $testCount = (string) $analysisResults->getCount(); |
||
38 | $test['tests'] = $testCount; |
||
39 | $test['failures'] = $testCount; |
||
40 | |||
41 | $suitCount = 0; |
||
42 | $caseCount = 0; |
||
43 | $oldRel = null; |
||
44 | $testsuite = null; |
||
45 | foreach ($analysisResults->getAnalysisResults() as $analysisResult) { |
||
46 | $details = $analysisResult->getFullDetails(); |
||
47 | $type = $details['type'] ?? 'error'; |
||
48 | $type = strtolower($type); |
||
49 | $column = $details['column'] ?? '0'; |
||
50 | |||
51 | $relativeFileName = $analysisResult->getLocation()->getRelativeFileName()->getFileName(); |
||
52 | |||
53 | if ($oldRel !== $relativeFileName || null === $testsuite) { |
||
54 | $testsuite = $test->addChild('testsuite'); |
||
55 | $testsuite->addAttribute('errors', '0'); |
||
56 | $testsuite->addAttribute('tests', (string) $caseCount); |
||
57 | $testsuite->addAttribute('failures', (string) $caseCount); |
||
58 | $testsuite->addAttribute('name', $relativeFileName); |
||
59 | |||
60 | $oldRel = $relativeFileName; |
||
61 | ++$suitCount; |
||
62 | $caseCount = 0; |
||
63 | } |
||
64 | |||
65 | $lineSprint = sprintf( |
||
66 | '%s at %s (%s:%s)', |
||
67 | $analysisResult->getType()->getType(), |
||
68 | $analysisResult->getLocation()->getAbsoluteFileName()->getFileName(), |
||
69 | (string) $analysisResult->getLocation()->getLineNumber()->getLineNumber(), |
||
70 | $column |
||
71 | ); |
||
72 | $testcase = $testsuite->addChild('testcase'); |
||
73 | $testcase->addAttribute('name', $lineSprint); |
||
74 | $testcase->addChild('failure'); |
||
75 | $testcase->failure->addAttribute('type', $type); |
||
76 | $testcase->failure->addAttribute( |
||
77 | 'message', |
||
78 | $analysisResult->getMessage() |
||
79 | ); |
||
80 | ++$caseCount; |
||
81 | $testsuite['tests'] = $caseCount; |
||
82 | $testsuite['failures'] = $caseCount; |
||
83 | } |
||
84 | |||
85 | $dom = new DOMDocument('1.0'); |
||
86 | $dom->preserveWhiteSpace = false; |
||
87 | $dom->formatOutput = true; |
||
88 | $asXml = $test->asXML(); |
||
89 | |||
90 | if (false !== $asXml) { |
||
91 | $dom->loadXML($asXml); |
||
|
|||
92 | } else { |
||
93 | throw new RuntimeException('xml could not be loaded'); |
||
94 | } |
||
95 | $saveXml = $dom->saveXML(); |
||
96 | if (false !== $saveXml) { |
||
97 | return $saveXml; |
||
98 | } |
||
99 | throw new RuntimeException('dom could not be saved'); |
||
100 | } |
||
120 |