Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
63.64% covered (warning)
63.64%
7 / 11
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ABRequirement
63.64% covered (warning)
63.64%
7 / 11
66.67% covered (warning)
66.67%
2 / 3
6.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isMet
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @since 1.35
21 */
22
23namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements;
24
25use MediaWiki\Config\Config;
26use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
27use MediaWiki\User\UserIdentity;
28
29/**
30 * @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
31 * @internal
32 */
33class ABRequirement implements Requirement {
34    private Config $config;
35
36    private UserIdentity $user;
37
38    /**
39     * The name of the experiment
40     */
41    private string $experimentName;
42
43    /**
44     * The name of the requirement
45     */
46    private string $name;
47
48    /**
49     * @param Config $config
50     * @param UserIdentity $user
51     * @param string $experimentName The name of the experiment
52     * @param string|null $name The name of the requirement
53     */
54    public function __construct(
55        Config $config,
56        UserIdentity $user,
57        string $experimentName,
58        ?string $name = null
59    ) {
60        $this->config = $config;
61        $this->user = $user;
62        $this->experimentName = $experimentName;
63        $this->name = $name ?? '';
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function getName(): string {
70        return $this->name;
71    }
72
73    /**
74     * Returns true if the user is logged-in and false otherwise.
75     *
76     * @inheritDoc
77     */
78    public function isMet(): bool {
79        // Get the experiment configuration from the config object.
80        $experiment = $this->config->get( 'VectorWebABTestEnrollment' );
81
82        // Use the local user ID directly
83        $id = $this->user->getId();
84
85        // Check if the experiment is not enabled or does not match the specified name.
86        if ( !$experiment['enabled'] || $experiment['name'] !== $this->experimentName ) {
87            // If the experiment is not enabled or does not match the specified name,
88            // return true, indicating that the metric is "met"
89            return true;
90        } else {
91            // If the experiment is enabled and matches the specified name,
92            // calculate the user's variant based on their user ID
93            $variant = $id % 2;
94
95            // Cast the variant value to a boolean and return it, indicating whether
96            // the user is in the "control" or "test" group.
97            return (bool)$variant;
98        }
99    }
100}