• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fractalzombie / frzb-request-mapper / 7325848574

26 Dec 2023 02:06AM UTC coverage: 86.877% (-0.4%) from 87.277%
7325848574

push

github

fractalzombie
[RequestMapper] Update Dependencies, update functional library with breaking changes

35 of 39 new or added lines in 26 files covered. (89.74%)

8 existing lines in 2 files now uncovered.

331 of 381 relevant lines covered (86.88%)

18.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/Helper/ClassHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2023 Mykhailo Shtanko fractalzombie@gmail.com
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15

16
namespace FRZB\Component\RequestMapper\Helper;
17

18
use Fp\Collections\ArrayList;
19
use FRZB\Component\RequestMapper\Exception\HelperException;
20
use JetBrains\PhpStorm\Immutable;
21

22
/** @internal */
23
#[Immutable]
24
final class ClassHelper
25
{
26
    private function __construct() {}
27

28
    public static function isNotBuiltinAndExists(string $className): bool
29
    {
30
        return (class_exists($className) || interface_exists($className))
46✔
31
            && !empty((new \ReflectionClass($className))->getNamespaceName())
46✔
32
            && !self::isEnum($className);
46✔
33
    }
34

35
    public static function isEnum(string $className): bool
36
    {
37
        return enum_exists($className);
46✔
38
    }
39

40
    public static function getShortName(string $className): string
41
    {
42
        try {
43
            return (new \ReflectionClass($className))->getShortName();
38✔
44
        } catch (\ReflectionException) {
14✔
45
            return $className;
14✔
46
        }
47
    }
48

49
    public static function isNameContains(string $className, string ...$haystack): bool
50
    {
51
        return ArrayList::collect($haystack)
22✔
52
            ->first(static fn (string $value): bool => StringHelper::contains(self::getShortName($className), $value))
22✔
53
            ->isSome()
22✔
54
        ;
22✔
55
    }
56

57
    /** @return \ReflectionParameter[] */
58
    public static function getMethodParameters(string $className, string $classMethod): array
59
    {
60
        try {
61
            return (new \ReflectionMethod($className, $classMethod))->getParameters();
16✔
62
        } catch (\ReflectionException) {
2✔
63
            return [];
2✔
64
        }
65
    }
66

67
    public static function getMethodParameter(string $className, string $classMethod, string $parameterName): \ReflectionParameter
68
    {
69
        return ArrayList::collect(self::getMethodParameters($className, $classMethod))
×
70
            ->first(static fn (\ReflectionParameter $property) => $property->getName() === $parameterName)
×
NEW
71
            ->getOrElse(fn () => throw HelperException::noMethodParameter($className, $classMethod, $parameterName))
×
72
        ;
×
73
    }
74

75
    /** @return \ReflectionProperty[] */
76
    public static function getProperties(string $className): array
77
    {
78
        try {
79
            return (new \ReflectionClass($className))->getProperties();
×
80
        } catch (\ReflectionException) {
×
81
            return [];
×
82
        }
83
    }
84

85
    public static function getProperty(string $className, string $propertyName): \ReflectionProperty
86
    {
87
        return ArrayList::collect(self::getProperties($className))
×
88
            ->first(static fn (\ReflectionProperty $property) => $property->getName() === $propertyName)
×
NEW
89
            ->getOrElse(fn () => throw HelperException::noClassProperty($className, $propertyName))
×
90
        ;
×
91
    }
92

93
    public static function isArrayHasAllPropertiesFromClass(array $array, string $class): bool
94
    {
95
        try {
96
            $rClass = new \ReflectionClass($class);
22✔
97
        } catch (\ReflectionException) {
2✔
98
            return false;
2✔
99
        }
100

101
        foreach ($rClass->getProperties() as $property) {
20✔
102
            $propertyValue = $array[$property->getName()] ?? $array[StringHelper::toSnakeCase($property->getName())] ?? null;
20✔
103

104
            if (!$propertyValue) {
20✔
105
                return false;
4✔
106
            }
107
        }
108

109
        return true;
16✔
110
    }
111

112
    /**
113
     * @template T
114
     *
115
     * @param class-string<T> $attributeClass
116
     *
117
     * @return null|T
118
     */
119
    public static function getAttribute(object|string $target, string $attributeClass): ?object
120
    {
121
        return ArrayList::collect(self::getAttributes($target, $attributeClass))
40✔
122
            ->firstElement()
40✔
123
            ->get()
40✔
124
        ;
40✔
125
    }
126

127
    /**
128
     * @template T
129
     *
130
     * @param class-string<T> $attributeClass
131
     *
132
     * @return array<T>
133
     */
134
    public static function getAttributes(object|string $target, string $attributeClass): array
135
    {
136
        try {
137
            $attributes = (new \ReflectionClass($target))->getAttributes($attributeClass);
40✔
138
        } catch (\ReflectionException) {
2✔
139
            $attributes = [];
2✔
140
        }
141

142
        return ArrayList::collect($attributes)
40✔
143
            ->map(static fn (\ReflectionAttribute $a) => $a->newInstance())
40✔
144
            ->toList()
40✔
145
        ;
40✔
146
    }
147
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc