Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit fa61157

Browse files
committed
Add test to verify method parameters type hint
1 parent 423d761 commit fa61157

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--TEST--
2+
Check whether all method parameters have valid type
3+
--SKIPIF--
4+
<?php if (!extension_loaded("v8")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$re = new ReflectionExtension('v8');
8+
9+
$classes = $re->getClasses();
10+
11+
12+
class Verifier
13+
{
14+
private $invalid = [];
15+
16+
public function verifyClass(ReflectionClass $class)
17+
{
18+
foreach ($class->getMethods() as $m) {
19+
$this->verifyMethod($m);
20+
}
21+
}
22+
23+
public function verifyMethod(ReflectionMethod $method)
24+
{
25+
foreach ($method->getParameters() as $p) {
26+
$this->verifyParameter($p);
27+
}
28+
}
29+
30+
public function verifyParameter(ReflectionParameter $parameter)
31+
{
32+
$type = $parameter->getType();
33+
34+
if (!$type || $type->isBuiltin()) {
35+
return;
36+
}
37+
38+
if (!(class_exists($type))) {
39+
$method_name = $parameter->getDeclaringClass()->getName() . '::' . $parameter->getDeclaringFunction()->getName();
40+
$param_name = $parameter->getName();
41+
42+
$shortcut = $method_name . '/' . $param_name;
43+
44+
if (isset($this->invalid[$shortcut])) {
45+
return;
46+
}
47+
48+
$this->invalid[$shortcut] = true;
49+
50+
echo "{$method_name}() method's parameter {$parameter->getName()} has invalid type ($type)", PHP_EOL;
51+
}
52+
}
53+
54+
public function isValid()
55+
{
56+
return empty($this->invalid);
57+
}
58+
}
59+
60+
$v = new Verifier();
61+
62+
63+
foreach ($classes as $c) {
64+
$v->verifyClass($c);
65+
}
66+
67+
if ($v->isValid()) {
68+
echo 'All method parameters are valid', PHP_EOL;
69+
}
70+
71+
?>
72+
--EXPECT--
73+
All method parameters are valid

0 commit comments

Comments
 (0)