99use ReflectionClass ;
1010use ReflectionMethod ;
1111
12+ use function sprintf ;
13+
1214trait Macroable
1315{
1416 /**
@@ -21,9 +23,8 @@ trait Macroable
2123 /**
2224 * Register a custom macro.
2325 *
24- * @param string $name Name.
25- * @param object|callable $macro Macro.
26- * @return void
26+ * @param string $name Name.
27+ * @param object|callable $macro Macro.
2728 */
2829 public static function macro (string $ name , $ macro ): void
2930 {
@@ -33,31 +34,31 @@ public static function macro(string $name, $macro): void
3334 /**
3435 * Mix another object into the class.
3536 *
36- * @param object $mixin Mixin.
37- * @param bool $replace Replace.
38- * @return void
37+ * @param object $mixin Mixin.
38+ * @param bool $replace Replace.
3939 *
4040 * @throws ReflectionException
4141 */
42- public static function mixin ($ mixin , bool $ replace = true ): void
42+ public static function mixin (object $ mixin , bool $ replace = true ): void
4343 {
4444 $ methods = (new ReflectionClass ($ mixin ))->getMethods (
4545 ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
4646 );
4747
4848 foreach ($ methods as $ method ) {
49- if ($ replace || ! static ::hasMacro ($ method ->name )) {
50- $ method ->setAccessible (true );
51- static ::macro ($ method ->name , $ method ->invoke ($ mixin ));
49+ if (! $ replace && static ::hasMacro ($ method ->name )) {
50+ continue ;
5251 }
52+
53+ $ method ->setAccessible (true );
54+ static ::macro ($ method ->name , $ method ->invoke ($ mixin ));
5355 }
5456 }
5557
5658 /**
5759 * Checks if macro is registered.
5860 *
59- * @param string $name Name
60- * @return bool
61+ * @param string $name Name
6162 */
6263 public static function hasMacro (string $ name ): bool
6364 {
@@ -67,8 +68,9 @@ public static function hasMacro(string $name): bool
6768 /**
6869 * Dynamically handle calls to the class.
6970 *
70- * @param string $method Method.
71- * @param array $parameters Parameters.
71+ * @param string $method Method.
72+ * @param array $parameters Parameters.
73+ *
7274 * @return mixed
7375 *
7476 * @throws BadMethodCallException
@@ -77,7 +79,9 @@ public static function __callStatic(string $method, array $parameters)
7779 {
7880 if (! static ::hasMacro ($ method )) {
7981 throw new BadMethodCallException (sprintf (
80- 'Method %s::%s does not exist. ' , static ::class, $ method
82+ 'Method %s::%s does not exist. ' ,
83+ static ::class,
84+ $ method
8185 ));
8286 }
8387
@@ -93,8 +97,9 @@ public static function __callStatic(string $method, array $parameters)
9397 /**
9498 * Dynamically handle calls to the class.
9599 *
96- * @param string $method Method.
97- * @param array $parameters Parameters.
100+ * @param string $method Method.
101+ * @param array $parameters Parameters.
102+ *
98103 * @return mixed
99104 *
100105 * @throws BadMethodCallException
@@ -103,7 +108,9 @@ public function __call(string $method, array $parameters)
103108 {
104109 if (! static ::hasMacro ($ method )) {
105110 throw new BadMethodCallException (sprintf (
106- 'Method %s::%s does not exist. ' , static ::class, $ method
111+ 'Method %s::%s does not exist. ' ,
112+ static ::class,
113+ $ method
107114 ));
108115 }
109116
0 commit comments