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

fractalzombie / frzb-request-mapper / 7325768402

26 Dec 2023 01:53AM UTC coverage: 86.877% (-0.4%) from 87.277%
7325768402

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%)

27.46 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))
69✔
31
            && !empty((new \ReflectionClass($className))->getNamespaceName())
69✔
32
            && !self::isEnum($className);
69✔
33
    }
34

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

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

49
    public static function isNameContains(string $className, string ...$haystack): bool
50
    {
51
        return ArrayList::collect($haystack)
33✔
52
            ->first(static fn (string $value): bool => StringHelper::contains(self::getShortName($className), $value))
33✔
53
            ->isSome()
33✔
54
        ;
33✔
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();
24✔
62
        } catch (\ReflectionException) {
3✔
63
            return [];
3✔
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);
33✔
97
        } catch (\ReflectionException) {
3✔
98
            return false;
3✔
99
        }
100

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

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

109
        return true;
24✔
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))
60✔
122
            ->firstElement()
60✔
123
            ->get()
60✔
124
        ;
60✔
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);
60✔
138
        } catch (\ReflectionException) {
3✔
139
            $attributes = [];
3✔
140
        }
141

142
        return ArrayList::collect($attributes)
60✔
143
            ->map(static fn (\ReflectionAttribute $a) => $a->newInstance())
60✔
144
            ->toList()
60✔
145
        ;
60✔
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

© 2026 Coveralls, Inc