@@ -10,6 +10,7 @@ This document details the rules available in the `Clarity` category.
1010| Rule | Code |
1111| :--- | :---------- |
1212| Explicit Octal | [ ` explicit-octal ` ] ( #explicit-octal ) |
13+ | Instanceof Stringable | [ ` instanceof-stringable ` ] ( #instanceof-stringable ) |
1314| Literal Named Argument | [ ` literal-named-argument ` ] ( #literal-named-argument ) |
1415| No Empty | [ ` no-empty ` ] ( #no-empty ) |
1516| No Hash Emoji | [ ` no-hash-emoji ` ] ( #no-hash-emoji ) |
@@ -59,6 +60,56 @@ $a = 0123;
5960```
6061
6162
63+ ## <a id =" instanceof-stringable " ></a >` instanceof-stringable `
64+
65+ Detects the legacy pattern ` is_object($x) && method_exists($x, '__toString') ` and suggests
66+ replacing it with ` $x instanceof Stringable ` for improved readability and performance.
67+
68+ Since PHP 8.0, all classes with ` __toString() ` automatically implement the ` Stringable ` interface.
69+
70+
71+ ### Requirements
72+
73+ - ** PHP version:** >= ` 8.0.0 `
74+
75+ ### Configuration
76+
77+ | Option | Type | Default |
78+ | :--- | :--- | :--- |
79+ | ` enabled ` | ` boolean ` | ` true ` |
80+ | ` level ` | ` string ` | ` "warning" ` |
81+
82+ ### Examples
83+
84+ #### Correct code
85+
86+ ``` php
87+ <?php
88+
89+ function stringify(mixed $value): string {
90+ if ($value instanceof Stringable) {
91+ return (string) $value;
92+ }
93+
94+ return '';
95+ }
96+ ```
97+
98+ #### Incorrect code
99+
100+ ``` php
101+ <?php
102+
103+ function stringify(mixed $value): string {
104+ if (is_object($value) && method_exists($value, '__toString')) {
105+ return (string) $value;
106+ }
107+
108+ return '';
109+ }
110+ ```
111+
112+
62113## <a id =" literal-named-argument " ></a >` literal-named-argument `
63114
64115Enforces that literal values used as arguments in function or method calls
0 commit comments