xref: /PHP-Parser/lib/PhpParser/Modifiers.php (revision 48f470ea)
1<?php declare(strict_types=1);
2
3namespace PhpParser;
4
5/**
6 * Modifiers used (as a bit mask) by various flags subnodes, for example on classes, functions,
7 * properties and constants.
8 */
9final class Modifiers {
10    public const PUBLIC    =  1;
11    public const PROTECTED =  2;
12    public const PRIVATE   =  4;
13    public const STATIC    =  8;
14    public const ABSTRACT  = 16;
15    public const FINAL     = 32;
16    public const READONLY  = 64;
17
18    public const VISIBILITY_MASK = 1 | 2 | 4;
19
20    /**
21     * @internal
22     */
23    public static function verifyClassModifier(int $a, int $b): void {
24        if ($a & Modifiers::ABSTRACT && $b & Modifiers::ABSTRACT) {
25            throw new Error('Multiple abstract modifiers are not allowed');
26        }
27
28        if ($a & Modifiers::FINAL && $b & Modifiers::FINAL) {
29            throw new Error('Multiple final modifiers are not allowed');
30        }
31
32        if ($a & Modifiers::READONLY && $b & Modifiers::READONLY) {
33            throw new Error('Multiple readonly modifiers are not allowed');
34        }
35
36        if ($a & 48 && $b & 48) {
37            throw new Error('Cannot use the final modifier on an abstract class');
38        }
39    }
40
41    /**
42     * @internal
43     */
44    public static function verifyModifier(int $a, int $b): void {
45        if ($a & Modifiers::VISIBILITY_MASK && $b & Modifiers::VISIBILITY_MASK) {
46            throw new Error('Multiple access type modifiers are not allowed');
47        }
48
49        if ($a & Modifiers::ABSTRACT && $b & Modifiers::ABSTRACT) {
50            throw new Error('Multiple abstract modifiers are not allowed');
51        }
52
53        if ($a & Modifiers::STATIC && $b & Modifiers::STATIC) {
54            throw new Error('Multiple static modifiers are not allowed');
55        }
56
57        if ($a & Modifiers::FINAL && $b & Modifiers::FINAL) {
58            throw new Error('Multiple final modifiers are not allowed');
59        }
60
61        if ($a & Modifiers::READONLY && $b & Modifiers::READONLY) {
62            throw new Error('Multiple readonly modifiers are not allowed');
63        }
64
65        if ($a & 48 && $b & 48) {
66            throw new Error('Cannot use the final modifier on an abstract class member');
67        }
68    }
69}
70