Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.32% covered (success)
90.32%
28 / 31
91.67% covered (success)
91.67%
11 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
MetricTrait
90.32% covered (success)
90.32%
28 / 31
91.67% covered (success)
91.67%
11 / 12
17.26
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getComponent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSamples
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSampleCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSampleRate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSampleRate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getLabelKeys
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLabel
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 setLabels
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
4.12
 copyToStatsdAt
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 fresh
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 * @file
18 */
19
20declare( strict_types=1 );
21
22namespace Wikimedia\Stats\Metrics;
23
24use InvalidArgumentException;
25use Psr\Log\LoggerInterface;
26use Wikimedia\Stats\Exceptions\IllegalOperationException;
27
28/**
29 * Implementation code common to all metric types.
30 *
31 * @internal
32 * @since 1.43
33 */
34trait MetricTrait {
35    /** @var BaseMetricInterface */
36    private BaseMetricInterface $baseMetric;
37
38    /** @var LoggerInterface */
39    private LoggerInterface $logger;
40
41    /** @inheritDoc */
42    public function __construct( $baseMetric, $logger ) {
43        $this->baseMetric = $baseMetric;
44        $this->logger = $logger;
45    }
46
47    /** @inheritDoc */
48    public function getName(): string {
49        return $this->baseMetric->getName();
50    }
51
52    /** @inheritDoc */
53    public function getComponent(): string {
54        return $this->baseMetric->getComponent();
55    }
56
57    /** @inheritDoc */
58    public function getSamples(): array {
59        return $this->baseMetric->getSamples();
60    }
61
62    /** @inheritDoc */
63    public function getSampleCount(): int {
64        return $this->baseMetric->getSampleCount();
65    }
66
67    /** @inheritDoc */
68    public function getSampleRate(): float {
69        return $this->baseMetric->getSampleRate();
70    }
71
72    /** @inheritDoc */
73    public function setSampleRate( float $sampleRate ) {
74        try {
75            $this->baseMetric->setSampleRate( $sampleRate );
76        } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
77            // Log the condition and give the caller something that will absorb calls.
78            trigger_error( $ex->getMessage(), E_USER_WARNING );
79            return new NullMetric;
80        }
81        return $this;
82    }
83
84    /** @inheritDoc */
85    public function getLabelKeys(): array {
86        return $this->baseMetric->getLabelKeys();
87    }
88
89    /** @inheritDoc */
90    public function setLabel( string $key, string $value ) {
91        try {
92            $this->baseMetric->addLabel( $key, $value );
93        } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
94            // Log the condition and give the caller something that will absorb calls.
95            trigger_error( $ex->getMessage(), E_USER_WARNING );
96            return new NullMetric;
97        }
98        return $this;
99    }
100
101    /** @inheritDoc */
102    public function setLabels( array $labels ) {
103        try {
104            foreach ( $labels as $key => $value ) {
105                $this->baseMetric->addLabel( $key, $value );
106            }
107        } catch ( IllegalOperationException | InvalidArgumentException $ex ) {
108            // Log the condition and give the caller something that will absorb calls.
109            trigger_error( $ex->getMessage(), E_USER_WARNING );
110            return new NullMetric;
111        }
112        return $this;
113    }
114
115    /** @inheritDoc */
116    public function copyToStatsdAt( $statsdNamespaces ) {
117        try {
118            $this->baseMetric->setStatsdNamespaces( $statsdNamespaces );
119        } catch ( InvalidArgumentException $ex ) {
120            // Log the condition and give the caller something that will absorb calls.
121            trigger_error( $ex->getMessage(), E_USER_WARNING );
122            return new NullMetric;
123        }
124        return $this;
125    }
126
127    /** @inheritDoc */
128    public function fresh(): self {
129        $this->baseMetric->clearLabels();
130        return $this;
131    }
132}