Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
22 / 24
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseFlags
91.67% covered (success)
91.67%
22 / 24
71.43% covered (warning)
71.43%
5 / 7
13.10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFlag
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 clearFlag
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 restoreFlags
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 getFlag
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 contains
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasImplicitTrxFlag
100.00% covered (success)
100.00%
1 / 1
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 *
18 * @file
19 */
20namespace Wikimedia\Rdbms\Database;
21
22use Wikimedia\Rdbms\DBLanguageError;
23
24/**
25 * @ingroup Database
26 * @internal
27 * @since 1.39
28 */
29class DatabaseFlags implements IDatabaseFlags {
30    /** @var int Current bit field of class DBO_* constants */
31    protected $flags;
32    /** @var int[] Prior flags member variable values */
33    private $priorFlags = [];
34
35    /** @var string[] List of DBO_* flags that can be changed after connection */
36    protected const MUTABLE_FLAGS = [
37        'DBO_DEBUG',
38        'DBO_NOBUFFER',
39        'DBO_TRX',
40        'DBO_DDLMODE',
41    ];
42    /** @var int Bit field of all DBO_* flags that can be changed after connection */
43    protected const DBO_MUTABLE = (
44        self::DBO_DEBUG | self::DBO_NOBUFFER | self::DBO_TRX | self::DBO_DDLMODE
45    );
46
47    public function __construct( $flags ) {
48        $this->flags = $flags;
49    }
50
51    public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
52        if ( $flag & ~static::DBO_MUTABLE ) {
53            throw new DBLanguageError(
54                "Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
55            );
56        }
57
58        if ( $remember === self::REMEMBER_PRIOR ) {
59            $this->priorFlags[] = $this->flags;
60        }
61
62        $this->flags |= $flag;
63    }
64
65    public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
66        if ( $flag & ~static::DBO_MUTABLE ) {
67            throw new DBLanguageError(
68                "Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
69            );
70        }
71
72        if ( $remember === self::REMEMBER_PRIOR ) {
73            $this->priorFlags[] = $this->flags;
74        }
75
76        $this->flags &= ~$flag;
77    }
78
79    public function restoreFlags( $state = self::RESTORE_PRIOR ) {
80        if ( !$this->priorFlags ) {
81            return;
82        }
83
84        if ( $state === self::RESTORE_INITIAL ) {
85            $this->flags = reset( $this->priorFlags );
86            $this->priorFlags = [];
87        } else {
88            $this->flags = array_pop( $this->priorFlags );
89        }
90    }
91
92    public function getFlag( $flag ) {
93        return ( ( $this->flags & $flag ) === $flag );
94    }
95
96    /**
97     * @param int $flags A bitfield of flags
98     * @param int $bit Bit flag constant
99     * @return bool Whether the bit field has the specified bit flag set
100     * @since 1.34
101     */
102    public static function contains( int $flags, int $bit ) {
103        return ( ( $flags & $bit ) === $bit );
104    }
105
106    public function hasImplicitTrxFlag() {
107        return $this->getFlag( self::DBO_TRX );
108    }
109
110}