|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace yaronius\ImplicitInterface; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use ReflectionClass; |
| 7 | +use ReflectionMethod; |
| 8 | +use ReflectionParameter; |
| 9 | +use ReflectionException; |
| 10 | +use ReflectionNamedType; |
| 11 | + |
| 12 | +/** |
| 13 | + * @param object|string $class |
| 14 | + * @param string $interface |
| 15 | + * @return bool |
| 16 | + * @throws ReflectionException |
| 17 | + */ |
| 18 | +function class_provides($class, string $interface): bool { |
| 19 | + // an object may be provided, so we handle this case here |
| 20 | + if (is_object($class)) { |
| 21 | + if ($class instanceof $interface) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + $class = get_class($class); |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + $classReflect = new ReflectionClass($class); |
| 29 | + } catch (ReflectionException $e) { |
| 30 | + //todo throw dedicated exception |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + // check if the class explicitly implements the interface |
| 35 | + if ($classReflect->implementsInterface($interface)) { |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + try { |
| 40 | + $interfaceReflect = new ReflectionClass($interface); |
| 41 | + } catch (ReflectionException $e) { |
| 42 | + //todo throw dedicated exception |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + if (!$interfaceReflect->isInterface()) { |
| 47 | + throw new InvalidArgumentException('Second argument must be an interface'); |
| 48 | + } |
| 49 | + |
| 50 | + foreach ($interfaceReflect->getMethods() as $interfaceMethod) { |
| 51 | + // check if class has same method name |
| 52 | + if (!$classReflect->hasMethod($interfaceMethod->getName())) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + $classMethod = $classReflect->getMethod($interfaceMethod->getName()); |
| 56 | + if (!reflection_methods_same($classMethod, $interfaceMethod)) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * @param ReflectionMethod $method1 |
| 66 | + * @param ReflectionMethod $method2 |
| 67 | + * @return bool |
| 68 | + */ |
| 69 | +function reflection_methods_same(ReflectionMethod $method1, ReflectionMethod $method2): bool |
| 70 | +{ |
| 71 | + $firstMethodModifiers = $method1->getModifiers(); |
| 72 | + $secondMethodModifiers = $method1->getModifiers(); |
| 73 | + if ($firstMethodModifiers != $secondMethodModifiers) { |
| 74 | + // interface methods are always abstract, so if it is the only difference, we ignore it |
| 75 | + if ($firstMethodModifiers ^ $secondMethodModifiers != ReflectionMethod::IS_ABSTRACT) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + if ($method1->getNumberOfParameters() != $method2->getNumberOfParameters()) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + if ($method1->getNumberOfRequiredParameters() != $method2->getNumberOfRequiredParameters()) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + $firstParams = $method1->getParameters(); |
| 86 | + $secondParams = $method2->getParameters(); |
| 87 | + /** |
| 88 | + * @var ReflectionParameter $interfaceParam |
| 89 | + * @var ReflectionParameter $classParam |
| 90 | + */ |
| 91 | + foreach ($secondParams as $i => $interfaceParam) { |
| 92 | + $classParam = $firstParams[$i]; |
| 93 | + if (!reflection_params_same($interfaceParam, $classParam)) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + } |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @param ReflectionParameter $param1 |
| 102 | + * @param ReflectionParameter $param2 |
| 103 | + * @return bool |
| 104 | + */ |
| 105 | +function reflection_params_same(ReflectionParameter $param1, ReflectionParameter $param2): bool |
| 106 | +{ |
| 107 | + if ($param1->isPassedByReference() != $param2->isPassedByReference()) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + if ($param1->isArray() != $param2->isArray()) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + if ($param1->isCallable() != $param2->isCallable()) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + if ($param1->isOptional() != $param2->isOptional()) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + if ($param1->isVariadic() != $param2->isVariadic()) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + $firstParamType = $param1->getType(); |
| 123 | + $secondParamType = $param2->getType(); |
| 124 | + |
| 125 | + if (is_null($firstParamType) || is_null($secondParamType)) { |
| 126 | + return $firstParamType === $secondParamType; |
| 127 | + } |
| 128 | + |
| 129 | + if (get_class($firstParamType) != get_class($secondParamType)) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + if ($firstParamType instanceof ReflectionNamedType && $secondParamType instanceof ReflectionNamedType) { |
| 133 | + if ($firstParamType->getName() != $secondParamType->getName()) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + if ($firstParamType->isBuiltin() != $secondParamType->isBuiltin()) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + if ($firstParamType->allowsNull() != $secondParamType->allowsNull()) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + return true; |
| 145 | +} |
0 commit comments