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

jojo1981 / php-types / 12707605573

10 Jan 2025 10:18AM UTC coverage: 88.119%. Remained the same
12707605573

push

github

jojo1981
Use GitHub actions workflow for CI instead of Travis CI.

267 of 303 relevant lines covered (88.12%)

16.43 hits per line

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

100.0
/src/ClassType.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the jojo1981/php-types package
4
 *
5
 * Copyright (c) 2020 Joost Nijhuis <jnijhuis81@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed in the root of the source code
9
 */
10
namespace Jojo1981\PhpTypes;
11

12
use Jojo1981\PhpTypes\Exception\TypeException;
13
use Jojo1981\PhpTypes\Value\ClassName;
14
use ReflectionClass;
15
use ReflectionException;
16
use function is_a;
17
use function is_subclass_of;
18
use function sprintf;
19

20
/**
21
 * @package Jojo1981\PhpTypes
22
 */
23
final class ClassType extends ObjectType
24
{
25
    /** @var ClassName */
26
    private ClassName $className;
27

28
    /**
29
     * @param ClassName $className
30
     * @throws TypeException
31
     */
32
    public function __construct(ClassName $className)
33
    {
34
        $this->assertClassName($className);
51✔
35
        $this->className = $className;
50✔
36
    }
37

38
    /**
39
     * @return ClassName
40
     */
41
    public function getClassName(): ClassName
42
    {
43
        return $this->className;
2✔
44
    }
45

46
    /**
47
     * @return string
48
     */
49
    public function getName(): string
50
    {
51
        return $this->className->getFqcn();
3✔
52
    }
53

54
    /**
55
     * @throws ReflectionException
56
     * @return ReflectionClass
57
     */
58
    public function getReflectionClass(): ReflectionClass
59
    {
60
        return new ReflectionClass($this->className->getFqcn());
1✔
61
    }
62

63
    /**
64
     * @return bool
65
     */
66
    public function isClass(): bool
67
    {
68
        return true;
1✔
69
    }
70

71
    /**
72
     * @param TypeInterface $type
73
     * @return bool
74
     */
75
    public function isAssignableType(TypeInterface $type): bool
76
    {
77
        return $type instanceof self && $this->isClassTypeAssignableToThisClassType($type);
1✔
78
    }
79

80
    /**
81
     * @param mixed $value
82
     * @return bool
83
     */
84
    public function isAssignableValue($value): bool
85
    {
86
        if (!parent::isAssignableValue($value)) {
1✔
87
            return false;
1✔
88
        }
89

90
        return is_a($value, $this->className->getFqcn());
1✔
91
    }
92

93
    /**
94
     * @param TypeInterface $type
95
     * @return bool
96
     */
97
    public function isEqual(TypeInterface $type): bool
98
    {
99
        if ($type instanceof self) {
1✔
100
            return $this->className->isEqual($type->getClassName());
1✔
101
        }
102

103
        return false;
1✔
104
    }
105

106
    /**
107
     * @param ClassName $className
108
     * @throws TypeException
109
     * @return void
110
     */
111
    private function assertClassName(ClassName $className): void
112
    {
113
        if (!$className->exists()) {
51✔
114
            throw new TypeException(sprintf('Class: `%s` doesn\'t exists', $className->getFqcn()));
2✔
115
        }
116
    }
117

118
    /**
119
     * @param ClassType $type
120
     * @return bool
121
     */
122
    private function isClassTypeAssignableToThisClassType(ClassType $type): bool
123
    {
124
        return $this->className->isEqual($type->getClassName())
1✔
125
            || is_subclass_of($type->getClassName()->getFqcn(), $this->className->getFqcn())
1✔
126
            || is_a($type->getClassName()->getFqcn(), $this->className->getFqcn());
1✔
127
    }
128
}
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