Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
VisibilityMethodPolicy
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 getVisibility
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 meetsRequirements
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Copyright (c) 2019-2023 NxtLvl Software Solutions.
7 *
8 * Freely available to use under the terms of the MIT license.
9 */
10
11namespace NxtLvlSoftware\StaticConstructors\Policy\Method\Visibility;
12
13use NxtLvlSoftware\StaticConstructors\Policy\Method\StaticConstructorMethodPolicy;
14use ReflectionMethod;
15
16/**
17 * Abstract method policy implementation for enforcing visibility requirements on
18 * static constructor methods.
19 *
20 * Inheriting classes should override the `VISIBILITY` constant to return a
21 * value from the {@link \NxtLvlSoftware\StaticConstructors\Policy\Method\Visibility\ConstructorVisibility}
22 * enum.
23 */
24abstract class VisibilityMethodPolicy implements StaticConstructorMethodPolicy {
25
26    protected const VISIBILITY = ConstructorVisibility::None;
27
28    /**
29     * Get the {@link \NxtLvlSoftware\StaticConstructors\Policy\Method\Visibility\ConstructorVisibility}
30     * enum value for this policy.
31     */
32    private static function getVisibility(): ConstructorVisibility {
33        return static::VISIBILITY;
34    }
35
36    /**
37     * Enforces that a method matches the visibility enforced by this policy.
38     */
39    final public static function meetsRequirements(ReflectionMethod $method): bool {
40        return match (self::getVisibility()) {
41            ConstructorVisibility::Private   => $method->isPrivate(),
42            ConstructorVisibility::Protected => $method->isProtected(),
43            ConstructorVisibility::Public    => $method->isPublic(),
44            ConstructorVisibility::None      => true
45        };
46    }
47
48}