diff --git a/src/ThemeFunctionDeprecationAnalyzer.php b/src/ThemeFunctionDeprecationAnalyzer.php index 9aa9681..4ad658c 100644 --- a/src/ThemeFunctionDeprecationAnalyzer.php +++ b/src/ThemeFunctionDeprecationAnalyzer.php @@ -41,7 +41,7 @@ final class ThemeFunctionDeprecationAnalyzer { } /** - * Analyzes theme functions in the extension. + * Analyzes theme functions in an extension. * * @param \Drupal\Core\Extension\Extension $extension * The extension to be analyzed. @@ -57,8 +57,9 @@ final class ThemeFunctionDeprecationAnalyzer { // If a theme is being analyzed, theme function overrides need to be // analyzed. if ($extension->getType() === 'theme') { - // Create new instance of theme registry to ensure that we have most recent - // data without having to make changes to the production theme registry. + // Create new instance of theme registry to ensure that we have the most + // recent data without having to make changes to the production theme + // registry. $theme_registry = new Registry($this->container->get('app.root'), new NullBackend('null'), $this->container->get('lock'), $this->container->get('module_handler'), $this->container->get('theme_handler'), $this->container->get('theme.initialization'), $extension->getName()); $theme_registry->setThemeManager($this->container->get('theme.manager')); $theme_hooks = $theme_registry->get(); @@ -69,9 +70,9 @@ final class ThemeFunctionDeprecationAnalyzer { $function = new \ReflectionFunction($extension->getName() . '_' . $machine_name); $file = $function->getFileName(); $line = $function->getStartLine(); - $deprecation_messages[$extension->getName() . '_' . $machine_name] = new DeprecationMessage(sprintf('The theme is overriding "%s" theme function. Theme functions are deprecated. For more info, see https://www.drupal.org/node/2575445.', $machine_name), $file, $line); + $deprecation_messages[$extension->getName() . '_' . $machine_name] = new DeprecationMessage(sprintf('The theme is overriding the "%s" theme function. Theme functions are deprecated. For more info, see https://www.drupal.org/node/2575445.', $machine_name), $file, $line); } catch (\ReflectionException $e) { - // This should never happen because drupal_find_theme_functions + // This should never happen because drupal_find_theme_functions() // ensures that the function exists. } } @@ -81,11 +82,11 @@ final class ThemeFunctionDeprecationAnalyzer { } /** - * Analyzes function for usages of theme functions. + * Analyzes function for definition of theme functions. * * This doesn't recognize functions in all edge cases. For example, theme - * functions could be generated dynamically in number of different ways.= - * However, this will be useful in most basic use cases. + * functions could be generated dynamically in a number of different ways. + * However, this will be useful in most use cases. * * @param $function * The function to be analyzed. @@ -95,10 +96,12 @@ final class ThemeFunctionDeprecationAnalyzer { * @return \Drupal\upgrade_status\DeprecationMessage[] */ private function analyzeFunction(string $function, Extension $extension): array { + $deprecation_messages = []; + try { $function_reflection = new \ReflectionFunction($function); } catch (\ReflectionException $e) { - // It is expected that the functions don't exist. + // Not all extensions implement theme hooks. return []; } @@ -106,26 +109,26 @@ final class ThemeFunctionDeprecationAnalyzer { try { $ast = $parser->parse(file_get_contents($function_reflection->getFileName())); } catch (Error $error) { - // Function cannot be evaluated because of a syntax error. - return []; + // The function cannot be evaluated because of a syntax error. + $deprecation_messages[] = new DeprecationMessage(sprintf('Parse error while processing the %s hook implementation.', $theme_function), $function_reflection->getFileName(), $node->getStartLine()); } if (!is_iterable($ast)) { return []; } $finder = new NodeFinder(); - // Find node for the function that is being analyzed. + // Find the node for the function that is being analyzed. $function_node = $finder->findFirst($ast, function (Node $node) use ($function) { return ($node instanceof Function_ && isset($node->name) && $node->name->name === $function); }); if (!$function_node) { - // This should never happen because the file has been loaded the based on - // the existence of the function. + // This should never happen because the file has been loaded based on the + // existence of the function. return []; } - // Find theme functions that has been defined using array syntax. + // Find theme functions that have been defined using the array syntax. // @code // function hook_theme() { // return [ @@ -136,14 +139,13 @@ final class ThemeFunctionDeprecationAnalyzer { $theme_function_nodes = $finder->find([$function_node], function(Node $node) { return ($node instanceof ArrayItem && $node->key instanceof String_ && $node->key->value === 'function'); }); - $deprecation_messages = []; foreach ($theme_function_nodes as $node) { $theme_function = $node->value instanceof String_ ? sprintf('"%s"', $node->value->value) : 'an unknown'; $deprecation_messages[] = new DeprecationMessage(sprintf('The %s is defining %s theme function. Theme functions are deprecated. For more info, see https://www.drupal.org/node/2575445.', $extension->getType(), $theme_function), $function_reflection->getFileName(), $node->getStartLine()); } - // Find theme functions that are being added to existing array using array - // square bracket syntax. + // Find theme functions that are being added to an existing array using + // the array square bracket syntax. // @code // function hook_theme_registry_alter(&$theme_registry) { // $theme_registry['theme_hook']['function'] = 'another_theme_function';