protected function _addImplicitUses($contents)
{
preg_match('/class\\s+\\S+(\\s+extends\\s+(\\S+))?(\\s+implements\\s+(\\S+))?/', $contents, $matches);
$toCheck = [];
if (isset($matches[2])) {
$toCheck[] = $matches[2];
}
if (isset($matches[4])) {
$toCheck[] = $matches[4];
}
preg_match_all('/function.*\\(.*\\b(\\S+)\\b\\s+\\$/', $contents, $matches);
$toCheck = array_filter(array_unique(array_merge($toCheck, $matches[1])));
preg_match_all('/\\b([A-Z][a-zA-Z0-9]+)::/', $contents, $matches);
$toCheck = array_filter(array_unique(array_merge($toCheck, $matches[1])));
foreach ($toCheck as $check) {
if (preg_match("/use .+\\b{$check};/", $contents)) {
continue;
}
if (!isset($this->implicitMap[$check])) {
$this->out(sprintf('<warning>%s is not in the implicit class map</warning>', $check));
continue;
}
$class = $this->implicitMap[$check];
$useStatement = "use {$class};\n";
$containsUseStatements = preg_match("/use .+;/", $contents);
$contents = preg_replace('/(namespace [\\S+]+;[\\n]{1,})/', '\\1' . $useStatement . (!$containsUseStatements ? "\n" : ''), $contents);
}
return $contents;
}