1<?php
2/*
3 * This file is part of the php-code-coverage package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\CodeCoverage\Report\Xml;
12
13use SebastianBergmann\CodeCoverage\Util;
14
15class Totals
16{
17    /**
18     * @var \DOMNode
19     */
20    private $container;
21
22    /**
23     * @var \DOMElement
24     */
25    private $linesNode;
26
27    /**
28     * @var \DOMElement
29     */
30    private $methodsNode;
31
32    /**
33     * @var \DOMElement
34     */
35    private $functionsNode;
36
37    /**
38     * @var \DOMElement
39     */
40    private $classesNode;
41
42    /**
43     * @var \DOMElement
44     */
45    private $traitsNode;
46
47    public function __construct(\DOMElement $container)
48    {
49        $this->container = $container;
50        $dom             = $container->ownerDocument;
51
52        $this->linesNode = $dom->createElementNS(
53            'http://schema.phpunit.de/coverage/1.0',
54            'lines'
55        );
56
57        $this->methodsNode = $dom->createElementNS(
58            'http://schema.phpunit.de/coverage/1.0',
59            'methods'
60        );
61
62        $this->functionsNode = $dom->createElementNS(
63            'http://schema.phpunit.de/coverage/1.0',
64            'functions'
65        );
66
67        $this->classesNode = $dom->createElementNS(
68            'http://schema.phpunit.de/coverage/1.0',
69            'classes'
70        );
71
72        $this->traitsNode = $dom->createElementNS(
73            'http://schema.phpunit.de/coverage/1.0',
74            'traits'
75        );
76
77        $container->appendChild($this->linesNode);
78        $container->appendChild($this->methodsNode);
79        $container->appendChild($this->functionsNode);
80        $container->appendChild($this->classesNode);
81        $container->appendChild($this->traitsNode);
82    }
83
84    public function getContainer()
85    {
86        return $this->container;
87    }
88
89    public function setNumLines($loc, $cloc, $ncloc, $executable, $executed)
90    {
91        $this->linesNode->setAttribute('total', $loc);
92        $this->linesNode->setAttribute('comments', $cloc);
93        $this->linesNode->setAttribute('code', $ncloc);
94        $this->linesNode->setAttribute('executable', $executable);
95        $this->linesNode->setAttribute('executed', $executed);
96        $this->linesNode->setAttribute(
97            'percent',
98            Util::percent($executed, $executable, true)
99        );
100    }
101
102    public function setNumClasses($count, $tested)
103    {
104        $this->classesNode->setAttribute('count', $count);
105        $this->classesNode->setAttribute('tested', $tested);
106        $this->classesNode->setAttribute(
107            'percent',
108            Util::percent($tested, $count, true)
109        );
110    }
111
112    public function setNumTraits($count, $tested)
113    {
114        $this->traitsNode->setAttribute('count', $count);
115        $this->traitsNode->setAttribute('tested', $tested);
116        $this->traitsNode->setAttribute(
117            'percent',
118            Util::percent($tested, $count, true)
119        );
120    }
121
122    public function setNumMethods($count, $tested)
123    {
124        $this->methodsNode->setAttribute('count', $count);
125        $this->methodsNode->setAttribute('tested', $tested);
126        $this->methodsNode->setAttribute(
127            'percent',
128            Util::percent($tested, $count, true)
129        );
130    }
131
132    public function setNumFunctions($count, $tested)
133    {
134        $this->functionsNode->setAttribute('count', $count);
135        $this->functionsNode->setAttribute('tested', $tested);
136        $this->functionsNode->setAttribute(
137            'percent',
138            Util::percent($tested, $count, true)
139        );
140    }
141}
142