Skip to content

Commit

Permalink
[4.0] Small refactor of typehint check in CMSPlugin (#30907)
Browse files Browse the repository at this point in the history
* Refactor typehint check

* Make private
  • Loading branch information
SharkyKZ authored Oct 8, 2020
1 parent 520e2e4 commit 25f8d40
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions libraries/src/Plugin/CMSPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,10 @@ public function registerListeners()

/** @var \ReflectionParameter $param */
$param = array_shift($parameters);
$typeHint = $param->getType();
$paramName = $param->getName();

// No type hint / type hint class not an event or parameter name is not "event"? It's a legacy listener.
if ($paramName !== 'event' || $typeHint === null || !$this->checkTypeHint($typeHint))
if ($paramName !== 'event' || !$this->parameterImplementsEventInterface($param))
{
$this->registerLegacyListener($method->name);

Expand Down Expand Up @@ -314,16 +313,25 @@ final protected function registerListener(string $methodName)
}

/**
* Used for checking if parameter is typehinted to accept \Joomla\Event\EventInterface, based on reflection type.
* Checks if parameter is typehinted to accept \Joomla\Event\EventInterface.
*
* @param \ReflectionType $reflectionType
* @param \ReflectionParameter $parameter
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
protected function checkTypeHint(\ReflectionType $reflectionType): bool
private function parameterImplementsEventInterface(\ReflectionParameter $parameter): bool
{
$reflectionType = $parameter->getType();

// Parameter is not typehinted.
if ($reflectionType === null)
{
return false;
}

// Parameter is nullable.
if ($reflectionType->allowsNull())
{
return false;
Expand Down

0 comments on commit 25f8d40

Please sign in to comment.