You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #23816 [Debug] Detect internal and deprecated methods (GuilhemN)
This PR was squashed before being merged into the 3.4 branch (closes#23816).
Discussion
----------
[Debug] Detect internal and deprecated methods
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | no
| New feature? | yes <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks? | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
I just realized we do not detect `@deprecated` methods like we do for `@final` methods, so here's a fix.
I reorganized the file to avoid code duplication... so the diff is kind of impressive but the behavior is the same.
Commits
-------
16eeabf [Debug] Detect internal and deprecated methods
@trigger_error(sprintf('The "%s" class is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".', $parent, self::$final[$parent], $name), E_USER_DEPRECATED);
if ($parent && isset(self::$finalMethods[$parent][$method->name])) {
194
-
@trigger_error(sprintf('%s It may change without further notice as of its next major version. You should not extend it from "%s".', self::$finalMethods[$parent][$method->name], $name), E_USER_DEPRECATED);
195
-
}
196
-
197
-
$doc = $method->getDocComment();
198
-
if (false === $doc || false === strpos($doc, '@final')) {
199
-
continue;
200
-
}
201
-
202
-
if (preg_match('#\n\s+\* @final(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$)#s', $doc, $notice)) {
self::$finalMethods[$name][$method->name] = sprintf('The "%s::%s()" method is considered final%s.', $name, $method->name, $message);
205
-
}
206
-
}
207
-
}
208
-
209
-
if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
210
-
@trigger_error(sprintf('The "%s" class uses the reserved name "%s", it will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
211
-
}
212
-
if (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
@trigger_error(sprintf('The "%s" class is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".', $parent, self::$final[$parent], $name), E_USER_DEPRECATED);
@trigger_error(sprintf('The "%s" %s %s "%s" that is deprecated%s.', $name, $type, $verb, $use, self::$deprecated[$use]), E_USER_DEPRECATED);
211
+
}
232
212
if (isset(self::$internal[$use]) && strncmp($ns, $use, $len)) {
233
213
@trigger_error(sprintf('The "%s" %s is considered internal%s. It may change without further notice. You should not use it from "%s".', $use, class_exists($use, false) ? 'class' : (interface_exists($use, false) ? 'interface' : 'trait'), self::$internal[$use], $name), E_USER_DEPRECATED);
234
214
}
235
215
}
236
216
237
-
if (!$parent || strncmp($ns, $parent, $len)) {
238
-
if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
239
-
@trigger_error(sprintf('The "%s" class extends "%s" that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
217
+
// Inherit @final and @deprecated annotations for methods
@trigger_error(sprintf('The "%s" method is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".', $methodShortName, $message, $name), E_USER_DEPRECATED);
238
+
}
239
+
240
+
foreach ($parentAndTraitsas$use) {
241
+
if (isset(self::$deprecatedMethods[$use][$method->name]) && strncmp($ns, $use, $len)) {
@trigger_error(sprintf('The "%s" method is considered internal%s. It may change without further notice. You should not use it from "%s".', $methodShortName, $message, $name), E_USER_DEPRECATED);
256
248
}
257
249
}
258
250
259
-
foreach ($deprecatedInterfacesas$interface) {
260
-
if (!isset($parentInterfaces[$interface])) {
261
-
@trigger_error(sprintf('The "%s" %s "%s" that is deprecated %s', $name, $refl->isInterface() ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
265
+
@trigger_error(sprintf('The "%s" class uses the reserved name "%s", it will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
266
+
}
265
267
}
266
268
267
269
if ($file) {
@@ -361,4 +363,31 @@ public function loadClass($class)
361
363
returntrue;
362
364
}
363
365
}
366
+
367
+
/**
368
+
* `class_implements` includes interfaces from the parents so we have to manually exclude them.
'message' => 'The "Symfony\Component\Debug\Tests\Fixtures\AnnotatedClass::deprecatedMethod()" method is deprecated since version 3.4. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsAnnotatedClass".',
'The "Symfony\Component\Debug\Tests\Fixtures\InternalTrait" trait is considered internal. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
328
351
'The "Symfony\Component\Debug\Tests\Fixtures\InternalClass" class is considered internal since version 3.4. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
329
352
'The "Symfony\Component\Debug\Tests\Fixtures\InternalInterface" interface is considered internal. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
330
-
'The "Symfony\Component\Debug\Tests\Fixtures\InternalTrait" trait is considered internal. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
353
+
'The "Symfony\Component\Debug\Tests\Fixtures\InternalClass::internalMethod()" method is considered internal since version 3.4. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
331
354
));
332
355
}
333
356
}
@@ -371,9 +394,15 @@ public function findFile($class)
371
394
eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
0 commit comments