Commit 59c2d109 authored by Daniel Cothran's avatar Daniel Cothran Committed by Daniel Cothran
Browse files

Issue #3268085 by andileco: Fix issues identified by PhpStan

parent 2d98c662
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ function charts_requirements($phase) {
 * Update existing default config.
 */
function charts_update_8301() {
  /** @var \Drupal\Core\Config\Config $default_config */
  /** @var \Drupal\Core\Config\Config $config */
  $config = \Drupal::service('config.factory')
    ->getEditable('charts.settings');

+15 −3
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\charts_api_example\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Uuid\UuidInterface;
@@ -26,6 +27,13 @@ class ChartsApiExample extends ControllerBase {
   */
  protected $uuidService;

  /**
   * The module extension list.
   *
   * @var \Drupal\Core\Extension\ModuleExtensionList
   */
  protected $moduleList;

  /**
   * Construct.
   *
@@ -33,10 +41,13 @@ class ChartsApiExample extends ControllerBase {
   *   The messenger service.
   * @param \Drupal\Component\Uuid\UuidInterface $uuidService
   *   The UUID service.
   * @param \Drupal\Core\Extension\ModuleExtensionList $module_list
   *   The module list.
   */
  public function __construct(MessengerInterface $messenger, UuidInterface $uuidService) {
  public function __construct(MessengerInterface $messenger, UuidInterface $uuidService, ModuleExtensionList $module_list) {
    $this->messenger = $messenger;
    $this->uuidService = $uuidService;
    $this->moduleList = $module_list;
  }

  /**
@@ -45,7 +56,8 @@ class ChartsApiExample extends ControllerBase {
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('messenger'),
      $container->get('uuid')
      $container->get('uuid'),
      $container->get('extension.list.module'),
    );
  }

@@ -328,7 +340,7 @@ class ChartsApiExample extends ControllerBase {
   * @return array $all_rows
   */
  private function getCsvContents() {
    $file_path = drupal_get_path('module', 'charts_api_example');
    $file_path = $this->moduleList->getPath('charts_api_example');
    $file_name = $file_path . '/fixtures/charts_api_example_file.csv';
    $handle = fopen($file_name, 'r');
    $all_rows = [];
+4 −1
Original line number Diff line number Diff line
@@ -55,7 +55,10 @@ function charts_billboard_find_library() {
  $searchdir[] = 'libraries';

  // Also search sites/<domain>/*.
  $searchdir[] = \Drupal::service('site.path') . '/libraries';
  $site_path = \Drupal::getContainer()->getParameter('site.path');
  $site_path = explode('/', $site_path);
  $site_name = $site_path[1];
  $searchdir[] = $site_name . '/libraries';

  foreach ($searchdir as $dir) {
    if (file_exists($dir . '/billboard/dist/billboard.min.js') || file_exists($dir . '/billboard/dist/billboard.js')) {
+42 −13
Original line number Diff line number Diff line
@@ -6,8 +6,11 @@ use Drupal\charts\Plugin\chart\Library\ChartBase;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Define a concrete class for a Chart.
@@ -17,7 +20,43 @@ use Drupal\Core\Url;
 *   name = @Translation("Billboard.js")
 * )
 */
class Billboard extends ChartBase {
class Billboard extends ChartBase implements ContainerFactoryPluginInterface {

  /**
   * The element info manager.
   *
   * @var \Drupal\Core\Render\ElementInfoManagerInterface
   */
  protected $elementInfo;

  /**
   * Constructs a \Drupal\views\Plugin\Block\ViewsBlockBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
   *   The element info manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ElementInfoManagerInterface $element_info) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->elementInfo = $element_info;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('element_info'),
    );
  }

  /**
   * {@inheritdoc}
@@ -204,15 +243,7 @@ class Billboard extends ChartBase {
   *   Return the chart definition.
   */
  private function populateAxes(array $element, array $chart_definition) {
    /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
    $element_info = \Drupal::service('element_info');
    $children = Element::children($element);
    /*
     * Setting defaults based on what Views uses. However, API users may
     * have different keys for their X and Y axes.
     */
    $x_axis_key = 'xaxis';
    $y_axis_key = 'yaxis';
    foreach ($children as $child) {
      $type = $element[$child]['#type'];
      if ($type === 'chart_xaxis') {
@@ -221,7 +252,7 @@ class Billboard extends ChartBase {
        $categories = $element[$x_axis_key]['#labels'] ? array_map('strip_tags', $element[$x_axis_key]['#labels']) : [];
        if (!in_array($chart_type, ['pie', 'donut'])) {
          if ($chart_type === 'scatter' || $chart_type === 'bubble') {
            //
            // Do nothing.
          }
          else {
            $chart_definition['data']['columns'][] = ['x'];
@@ -262,8 +293,6 @@ class Billboard extends ChartBase {
  private function populateData(array &$element, array $chart_definition) {
    $type = $this->getType($element['#chart_type']);
    $types = [];
    /** @var \Drupal\Core\Render\ElementInfoManagerInterface $element_info */
    $element_info = \Drupal::service('element_info');
    $children = Element::children($element);
    $y_axes = [];
    foreach ($children as $child) {
@@ -283,7 +312,7 @@ class Billboard extends ChartBase {
      $child_element = $element[$key];
      // Make sure defaults are loaded.
      if (empty($child_element['#defaults_loaded'])) {
        $child_element += $element_info->getInfo($child_element['#type']);
        $child_element += $this->elementInfo->getInfo($child_element['#type']);
      }
      if ($child_element['#color'] && $type !== 'gauge') {
        $chart_definition['color']['pattern'][] = $child_element['#color'];
+13 −4
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\charts_blocks\Plugin\Block;

use Drupal\charts\Element\Chart;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
@@ -27,13 +28,21 @@ class ChartsBlock extends BlockBase implements ContainerFactoryPluginInterface {
   */
  protected $configFactory;

  /**
   * The UUID service.
   *
   * @var \Drupal\Component\Uuid\UuidInterface
   */
  protected $uuidService;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, UuidInterface $uuidService) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->setConfiguration($configuration);
    $this->configFactory = $config_factory;
    $this->uuidService = $uuidService;
  }

  /**
@@ -44,7 +53,8 @@ class ChartsBlock extends BlockBase implements ContainerFactoryPluginInterface {
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('config.factory')
      $container->get('config.factory'),
      $container->get('uuid'),
    );
  }

@@ -93,8 +103,7 @@ class ChartsBlock extends BlockBase implements ContainerFactoryPluginInterface {

    // Creates a UUID for the chart ID.
    $chart_id = 'charts_block__' . $this->configuration['id'];
    $uuid_service = \Drupal::service('uuid');
    $id = 'chart-' . $uuid_service->generate();
    $id = 'chart-' . $this->uuidService->generate();
    $build = Chart::buildElement($chart_settings, $chart_id);
    $build['#id'] = $id;
    $build['#chart_id'] = $chart_id;
Loading