wiki.techinc.nl/includes/libs/DebugInfo/AnnotationReader.php
Tim Starling 94e20db64b Add some helpers to filter properties out of var_dump()
Dumping many kinds of object with var_dump() leads to dumping the whole
address space, because a chain of references leads to MediaWikiServices.

So, introduce a trait which replaces the problematic properties with a
placeholder, if their doc comment contains @noVarDump.

Bug: T277618
Change-Id: Ifa685c26fbc5317d0359544289ec3f433ec4fedf
2022-10-12 12:42:23 -04:00

38 lines
935 B
PHP

<?php
namespace Wikimedia\DebugInfo;
/**
* Utility class for reading doc comment annotations
*
* @since 1.40
*/
class AnnotationReader {
/** @var bool[] */
private static $cache = [];
/**
* Determine whether a ReflectionProperty has a specified annotation
*
* @param \ReflectionProperty $property
* @param string $annotationName
* @return bool
*/
public static function propertyHasAnnotation(
\ReflectionProperty $property,
string $annotationName
) {
$cacheKey = "$annotationName@{$property->class}::{$property->name}";
if ( !isset( self::$cache[$cacheKey] ) ) {
$comment = $property->getDocComment();
if ( $comment === false ) {
self::$cache[$cacheKey] = false;
} else {
$encAnnotation = preg_quote( $annotationName, '!' );
self::$cache[$cacheKey] =
(bool)preg_match( "!^[ \t]*(/\*\*|\*)[ \t]*@$encAnnotation\b!im", $comment );
}
}
return self::$cache[$cacheKey];
}
}