Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit b885d09

Browse files
Add method helper
1 parent a328231 commit b885d09

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

src/Classes/MethodHelper.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace SebastiaanLuca\PhpHelpers\Classes;
4+
5+
use Error;
6+
use ReflectionException;
7+
use ReflectionMethod;
8+
9+
class MethodHelper
10+
{
11+
/**
12+
* Check if an object has a given method of a given type.
13+
*
14+
* @param object $object
15+
* @param string $method
16+
* @param string $type
17+
*
18+
* @return bool
19+
*/
20+
public static function hasMethodOfType($object, $method, $type) : bool
21+
{
22+
if (! method_exists($object, $method)) {
23+
return false;
24+
}
25+
26+
try {
27+
$reflection = new ReflectionMethod($object, $method);
28+
// FIXME
29+
$type = 'is' . studly_case($type);
30+
31+
return $reflection->{$type}();
32+
} catch (ReflectionException $exception) {
33+
return false;
34+
} catch (Error $exception) {
35+
return false;
36+
}
37+
}
38+
39+
/**
40+
* Check if an object has a given protected method.
41+
*
42+
* @param object $object
43+
* @param string $method
44+
*
45+
* @return bool
46+
*/
47+
public static function hasProtectedMethod($object, $method) : bool
48+
{
49+
return static::hasMethodOfType($object, $method, 'protected');
50+
}
51+
52+
/**
53+
* Check if an object has a given public method.
54+
*
55+
* @param object $object
56+
* @param string $method
57+
*
58+
* @return bool
59+
*/
60+
public static function hasPublicMethod($object, $method) : bool
61+
{
62+
return static::hasMethodOfType($object, $method, 'public');
63+
}
64+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace SebastiaanLuca\Helpers\Tests\Unit\Classes;
4+
5+
use SebastiaanLuca\PhpHelpers\Classes\MethodHelper;
6+
use SebastiaanLuca\PhpHelpers\Tests\TestCase;
7+
8+
class MethodHelperTest extends TestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
public function it detects if a method of a certain visibility exists() : void
14+
{
15+
$class = new class
16+
{
17+
private function myMethod() : bool
18+
{
19+
return true;
20+
}
21+
};
22+
23+
$this->assertTrue(MethodHelper::hasMethodOfType($class, 'myMethod', 'private'));
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
public function it detects a missing method() : void
30+
{
31+
$class = new class
32+
{
33+
protected function myMethod() : bool
34+
{
35+
return true;
36+
}
37+
};
38+
39+
$this->assertFalse(MethodHelper::hasMethodOfType($class, 'myInvalidMethod', 'protected'));
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function it detects a method with different visibility() : void
46+
{
47+
$class = new class
48+
{
49+
public function myMethod() : bool
50+
{
51+
return true;
52+
}
53+
};
54+
55+
$this->assertFalse(MethodHelper::hasMethodOfType($class, 'myMethod', 'private'));
56+
}
57+
58+
/**
59+
* @test
60+
*/
61+
public function it detects a protected method() : void
62+
{
63+
$class = new class
64+
{
65+
protected function myMethod() : bool
66+
{
67+
return true;
68+
}
69+
};
70+
71+
$this->assertTrue(MethodHelper::hasProtectedMethod($class, 'myMethod'));
72+
}
73+
74+
/**
75+
* @test
76+
*/
77+
public function it detects a public method() : void
78+
{
79+
$class = new class
80+
{
81+
public function myMethod() : bool
82+
{
83+
return true;
84+
}
85+
};
86+
87+
$this->assertTrue(MethodHelper::hasPublicMethod($class, 'myMethod'));
88+
}
89+
}

0 commit comments

Comments
 (0)