Completed
Push — develop ( 80740b...61b5c3 )
by Mike
10:20
created

Statistics::getErrorCounter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Transformer\Writer;
17
18
use phpDocumentor\Application;
19
use phpDocumentor\Descriptor\DescriptorAbstract;
20
use phpDocumentor\Descriptor\FileDescriptor;
21
use phpDocumentor\Descriptor\ProjectDescriptor;
22
use phpDocumentor\Transformer\Transformation;
23
24
/**
25
 * Statistics transformation writer; generates statistic report as XML.
26
 *
27
 * Generated XML structure:
28
 * ```
29
 *  <?xml version="1.0"?>
30
 *  <phpdoc-stats version="2.4.0">
31
 *    <stat date="2018-06-02T19:26:15+02:00">
32
 *      <counters>
33
 *        <deprecated>100</deprecated>
34
 *        <errors>377</errors>
35
 *        <markers>2</markers>
36
 *      </counters>
37
 *    </stat>
38
 *  </phpdoc-stats>
39
 * ```
40
 *
41
 * @author Siad Ardroumli <[email protected]>
42
 */
43
class Statistics extends Checkstyle
44
{
45
    /**
46
     * This method generates the checkstyle.xml report
47
     *
48
     * @param ProjectDescriptor $project        Document containing the structure.
49
     * @param Transformation    $transformation Transformation to execute.
50
     */
51
    public function transform(ProjectDescriptor $project, Transformation $transformation)
52
    {
53
        $artifact = $this->getDestinationPath($transformation);
54
55
        $now = new \DateTime('now');
56
        $date = $now->format(DATE_ATOM);
57
58
        $document = new \DOMDocument();
59
        $document->formatOutput = true;
60
        $document->preserveWhiteSpace = false;
61
62
        if (is_file($artifact)) {
63
            $document->load($artifact);
64
        } else {
65
            $document = $this->appendPhpdocStatsElement($document);
66
        }
67
68
        $document = $this->appendStatElement($document, $project, $date);
69
70
        $this->saveCheckstyleReport($artifact, $document);
71
    }
72
73
    /**
74
     * Append phpdoc-stats element to the document.
75
     *
76
     * @return \DOMDocument
77
     */
78
    protected function appendPhpdocStatsElement(\DOMDocument $document)
79
    {
80
        $stats = $document->createElement('phpdoc-stats');
81
        $stats->setAttribute('version', Application::VERSION());
82
        $document->appendChild($stats);
83
84
        return $document;
85
    }
86
87
    /**
88
     * Appends a stat fragment.
89
     *
90
     * @param string $date
91
     * @return \DOMDocument
92
     */
93
    protected function appendStatElement(\DOMDocument $document, ProjectDescriptor $project, $date)
94
    {
95
        $stat = $document->createDocumentFragment();
96
        $stat->appendXML(
97
            <<<STAT
98
<stat date="${date}">
99
    <counters>
100
        <files>{$this->getFilesCounter($project)}</files>
101
        <deprecated>{$this->getDeprecatedCounter($project)}</deprecated>
102
        <errors>{$this->getErrorCounter($project)}</errors>
103
        <markers>{$this->getMarkerCounter($project)}</markers>
104
    </counters>
105
</stat>
106
STAT
107
        );
108
        $document->documentElement->appendChild($stat);
109
110
        return $document;
111
    }
112
113
    /**
114
     * Get number of files.
115
     *
116
     * @return int
117
     */
118
    protected function getFilesCounter(ProjectDescriptor $project)
119
    {
120
        return $project->getFiles()->count();
121
    }
122
123
    /**
124
     * Get number of deprecated elements.
125
     *
126
     * @return int
127
     */
128
    protected function getDeprecatedCounter(ProjectDescriptor $project)
129
    {
130
        $deprecatedCounter = 0;
131
132
        /** @var DescriptorAbstract $element */
133
        foreach ($project->getIndexes()->get('elements') as $element) {
134
            if ($element->isDeprecated()) {
135
                ++$deprecatedCounter;
136
            }
137
        }
138
139
        return $deprecatedCounter;
140
    }
141
142
    /**
143
     * Get number of errors.
144
     *
145
     * @return int
146
     */
147
    protected function getErrorCounter(ProjectDescriptor $project)
148
    {
149
        $errorCounter = 0;
150
151
        /* @var FileDescriptor $fileDescriptor */
152
        foreach ($project->getFiles()->getAll() as $fileDescriptor) {
153
            $errorCounter += count($fileDescriptor->getAllErrors()->getAll());
154
        }
155
156
        return $errorCounter;
157
    }
158
159
    /**
160
     * Get number of markers.
161
     *
162
     * @return int
163
     */
164
    protected function getMarkerCounter(ProjectDescriptor $project)
165
    {
166
        $markerCounter = 0;
167
168
        /* @var $fileDescriptor FileDescriptor */
169
        foreach ($project->getFiles()->getAll() as $fileDescriptor) {
170
            $markerCounter += $fileDescriptor->getMarkers()->count();
171
        }
172
173
        return $markerCounter;
174
    }
175
}
176