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

phpolar / model / 15811103117

22 Jun 2025 09:45PM UTC coverage: 89.003% (-2.8%) from 91.815%
15811103117

push

github

web-flow
Merge pull request #130 from phpolar/merge-core

Merge core

3 of 10 new or added lines in 4 files covered. (30.0%)

2 existing lines in 1 file now uncovered.

259 of 291 relevant lines covered (89.0%)

3.66 hits per line

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

93.94
/src/FormControlTypeDetectionTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Phpolar\Model;
6

7
use DateTime;
8
use DateTimeImmutable;
9
use DateTimeInterface;
10
use ReflectionNamedType;
11
use ReflectionProperty;
12
use ReflectionUnionType;
13

14
/**
15
 * Adds support for detecting the form control type based on the type declaration of the property.
16
 *
17
 * If the property type is not declared, the type of the value of the property will be used.
18
 */
19
trait FormControlTypeDetectionTrait
20
{
21
    /**
22
     * Uses the type declaration of the property to determine the form control type of the field.
23
     *
24
     * @api
25
     */
26
    public function getFormControlType(string $propName): FormControlTypes
27
    {
28
        $property = new ReflectionProperty($this, $propName);
1✔
29
        $propertyType = $property->getType();
1✔
30
        $propTypeNotDeclared = $propertyType === null;
1✔
31
        if ($propTypeNotDeclared === true) {
1✔
32
            return $property->isInitialized($this) === true ? match (gettype($property->getValue($this))) {
1✔
33
                "string",
1✔
34
                "integer",
1✔
35
                "double",
1✔
36
                "boolean" => FormControlTypes::Input,
1✔
37
                "object" => match (get_class($property->getValue($this))) {
1✔
38
                    "DateTimeInterface", "DateTimeImmutable", "DateTime" => FormControlTypes::Input,
1✔
39
                    default => FormControlTypes::Invalid,
1✔
40
                },
1✔
41
                "array" => FormControlTypes::Select,
1✔
42
                default => FormControlTypes::Invalid,
1✔
43
            } : FormControlTypes::Invalid;
1✔
44
        }
45
        if ($propertyType instanceof ReflectionNamedType) {
1✔
46
            return match ($property->getName()) {
1✔
47
                "string",
1✔
48
                "int",
1✔
49
                "float",
1✔
50
                "bool",
1✔
51
                DateTimeInterface::class,
1✔
52
                DateTimeImmutable::class,
1✔
53
                DateTime::class =>
1✔
UNCOV
54
                FormControlTypes::Input,
×
UNCOV
55
                "array" => FormControlTypes::Select,
×
56
                default => FormControlTypes::Invalid,
1✔
57
            };
1✔
58
        }
59
        if ($propertyType instanceof ReflectionUnionType) {
1✔
60
            return in_array("string", $propertyType->getTypes()) === true &&
1✔
61
                in_array("array", $propertyType->getTypes()) === false ? FormControlTypes::Input : FormControlTypes::Invalid;
1✔
62
        }
63
        return FormControlTypes::Invalid;
1✔
64
    }
65
}
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