You are here

class Functions in Drupal 7 to 8/9 Module Upgrader 8

Plugin annotation


@Indexer(
 id = "function"
)

Hierarchy

Expanded class hierarchy of Functions

6 files declare their use of Functions
DeleteTest.php in tests/src/Unit/Plugin/DMU/Fixer/DeleteTest.php
FlagHookTest.php in tests/src/Unit/Plugin/DMU/Analyzer/FlagHookTest.php
FunctionsTest.php in tests/src/Unit/Plugin/DMU/Indexer/FunctionsTest.php
HookFormAlterTest.php in tests/src/Unit/Plugin/DMU/Analyzer/HookFormAlterTest.php
HookPermissionTest.php in tests/src/Unit/Plugin/DMU/Analyzer/HookPermissionTest.php

... See full list

File

src/Plugin/DMU/Indexer/Functions.php, line 21

Namespace

Drupal\drupalmoduleupgrader\Plugin\DMU\Indexer
View source
class Functions extends IndexerBase implements IndexerExecutionInterface, IndexerUsageInterface {
  protected function prepareID($id) {
    return preg_replace('/^hook_/', $this->target
      ->id() . '_', $id);
  }

  /**
   * {@inheritdoc}
   */
  public function has($identifier) {
    return parent::has($this
      ->prepareID($identifier));
  }

  /**
   * {@inheritdoc}
   */
  public function hasAny(array $identifiers) {
    return parent::hasAny(array_map([
      $this,
      'prepareID',
    ], $identifiers));
  }

  /**
   * {@inheritdoc}
   */
  public function hasAll(array $identifiers) {
    return parent::hasAll(array_map([
      $this,
      'prepareID',
    ], $identifiers));
  }

  /**
   * {@inheritdoc}
   */
  public function addFile($path) {
    if (!class_exists('Pharborist\\Parser')) {
      \Drupal::logger("Drupalmoduleupgrader")
        ->error("Have you ran 'composer up' in the drupalmoduleupgrader folder yet?", [
        "missing",
      ]);
      throw new \Exception("The Pharborist\\Parser class was not found, please make sure to run 'composer up' in the drupalmoduleupgrader folder and try again.");
    }
    $doc = Parser::parseFile($path);
    $doc
      ->children(Filter::isInstanceOf('\\Pharborist\\Functions\\FunctionDeclarationNode'))
      ->each([
      $this,
      'add',
    ]);
    $doc
      ->find(Filter::isInstanceOf('\\Pharborist\\Functions\\FunctionCallNode'))
      ->each([
      $this,
      'add',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function add(NodeInterface $node) {

    /** @var \Pharborist\Functions\FunctionDeclarationNode|\Pharborist\Functions\FunctionCallNode $node */
    $fields = [
      'id' => (string) $node
        ->getName(),
      'file' => $node
        ->getFilename(),
      'type' => get_class($node),
    ];
    if ($node instanceof FunctionDeclarationNode) {
      $logical = new ContainsLogicFilter();
      $logical
        ->whitelist('t');
      $logical
        ->whitelist('drupal_get_path');
      $fields['has_logic'] = (int) $node
        ->is($logical);
    }
    $this->db
      ->insert($this->table)
      ->fields($fields)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function delete($id) {
    parent::delete($this
      ->prepareID($id));
  }

  /**
   * {@inheritdoc}
   */
  public function get($identifier) {
    $identifier = $this
      ->prepareID($identifier);
    $file = $this
      ->getQuery([
      'file',
    ])
      ->condition('id', $identifier)
      ->execute()
      ->fetchField();
    return $this->target
      ->open($file)
      ->children(Filter::isFunction($identifier))
      ->get(0);
  }

  /**
   * {@inheritdoc}
   */
  public function getMultiple(array $identifiers) {
    return parent::getMultiple(array_map([
      $this,
      'prepareID',
    ], $identifiers));
  }

  /**
   * {@inheritdoc}
   */
  public function getFields() {
    $fields = parent::getFields();
    $fields['type'] = [
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
    ];
    $fields['has_logic'] = [
      'type' => 'int',
      'size' => 'tiny',
      'unsigned' => TRUE,
    ];
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getQuery(array $fields = []) {
    return parent::getQuery($fields)
      ->condition('type', 'Pharborist\\Functions\\FunctionDeclarationNode');
  }

  /**
   * {@inheritdoc}
   */
  public function hasExecutable($identifier) {
    if ($this
      ->has($identifier)) {
      $ret = $this
        ->getQuery()
        ->condition('id', $this
        ->prepareID($identifier))
        ->condition('has_logic', 0)
        ->countQuery()
        ->execute()
        ->fetchField();
      return $ret;
    }
    else {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function execute($identifier, array $arguments = []) {
    $function = $this
      ->prepareID($identifier);

    // If the function already exists, we can safely assume that it's already
    // been scanned for dangerous logic and evaluated into existence.
    if (function_exists($function)) {
      return call_user_func_array($function, $arguments);
    }
    else {
      if ($this
        ->hasExecutable($function)) {
        eval($this
          ->get($function)
          ->get(0)
          ->getText());
        return $this
          ->execute($function, $arguments);
      }
      else {
        $variables = [
          '@function' => $function,
        ];
        throw new \LogicException((new FormattableMarkup('Cowardly refusing to execute @function.', $variables))
          ->__toString());
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getUsages($identifier) {
    $function = $this
      ->prepareID($identifier);
    $files = $this
      ->getQuery([
      'file',
    ])
      ->distinct()
      ->condition('id', $function)
      ->condition('type', 'Pharborist\\Functions\\FunctionCallNode')
      ->execute()
      ->fetchCol();
    $usages = new NodeCollection();
    foreach ($files as $file) {
      $this->target
        ->open($file)
        ->find(Filter::isFunctionCall($function))
        ->addTo($usages);
    }
    return $usages;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
Functions::add public function Overrides IndexerBase::add
Functions::addFile public function Overrides IndexerInterface::addFile
Functions::delete public function Overrides IndexerBase::delete
Functions::execute public function Overrides IndexerExecutionInterface::execute
Functions::get public function Overrides IndexerInterface::get
Functions::getFields public function Overrides IndexerBase::getFields
Functions::getMultiple public function Overrides IndexerBase::getMultiple
Functions::getQuery public function Overrides IndexerBase::getQuery
Functions::getUsages public function Overrides IndexerUsageInterface::getUsages
Functions::has public function Overrides IndexerBase::has
Functions::hasAll public function Overrides IndexerBase::hasAll
Functions::hasAny public function Overrides IndexerBase::hasAny
Functions::hasExecutable public function Returns if the specified index object can be evaluated and executed safely. Overrides IndexerExecutionInterface::hasExecutable
Functions::prepareID protected function
IndexerBase::$db protected property
IndexerBase::$table protected property
IndexerBase::$target protected property
IndexerBase::bind public function Overrides IndexerInterface::bind
IndexerBase::build public function Overrides IndexerInterface::build 1
IndexerBase::clear public function Overrides IndexerInterface::clear
IndexerBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
IndexerBase::deleteFile public function Overrides IndexerInterface::deleteFile
IndexerBase::destroy public function Overrides IndexerInterface::destroy
IndexerBase::getAll public function Overrides IndexerInterface::getAll 1
IndexerBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.