GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

df_tools_image_seed_derivatives_operations()   C
last analyzed

Complexity

Conditions 14
Paths 30

Size

Total Lines 64
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 64
rs 6.2666
c 0
b 0
f 0
cc 14
nc 30
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 * Code for the df_tools_image module.
6
 */
7
8
9
/**
10
 * Seeds image style derivatives.
11
 *
12
 * @param string $dir The root level of images
13
 * @param array $excludes Image styles to exclude
14
 * @param array $includes Image styles to include
15
 */
16
function df_tools_image_seed_derivatives($dir = '/', $excludes = array(), $includes = array()) {
17
  if (!\Drupal::moduleHandler()->moduleExists('image')) {
18
    drupal_set_message("This script requires the image module to be enabled.", 'error');
0 ignored issues
show
Deprecated Code introduced by
The function drupal_set_message() has been deprecated: in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

18
    /** @scrutinizer ignore-deprecated */ drupal_set_message("This script requires the image module to be enabled.", 'error');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
19
    return;
20
  }
21
22
  $operations = df_tools_image_seed_derivatives_operations($dir, $excludes, $includes);
23
24
  $batch = [
25
    'operations' => $operations,
26
    'title' => t('Image styles processing'),
27
    'init_message' => t('Starting image style processing'),
28
    'progress_message' => t('Processing image styles...'),
29
    'error_message' => t('An error occurred. Some or all of the image style processing has failed.'),
30
  ];
31
32
  batch_set($batch);
33
  if (PHP_SAPI === 'cli') {
34
    drush_backend_batch_process();
35
  }
36
}
37
38
/**
39
 * Returns the operations required to seed image style derivatives.
40
 *
41
 * @param string $dir The root level of images
42
 * @param array $excludes Image styles to exclude
43
 * @param array $includes Image styles to include
44
 *
45
 * @return array Operations required to seed image derivatives
46
 */
47
function df_tools_image_seed_derivatives_operations($dir = '/', $excludes = array(), $includes = array()) {
48
  $operations = array();
49
50
  $extensions = array('jpeg', 'jpg', 'gif', 'png');
51
  $mimetypes = array('image/jpeg', 'image/jpg', 'image/gif', 'image/png');
52
53
  $dir = rtrim($dir, '/');
54
  if ($dir == 'public') {
55
    $file_pattern = "[^\/]*"; // Finds anything that does not contain "/", should be fine.
56
  }
57
  else {
58
    $file_pattern = $dir ? $dir . ".+" : ".+";
59
  }
60
61
  $regex = "^public:\/\/(" . $file_pattern . ")\.(" . implode($extensions, '|') . ")$";
0 ignored issues
show
Unused Code introduced by
The call to implode() has too many arguments starting with '|'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
  $regex = "^public:\/\/(" . $file_pattern . ")\.(" . /** @scrutinizer ignore-call */ implode($extensions, '|') . ")$";

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
62
63
  // Query the database for files that match this pattern.
64
  $query = db_select('file_managed', 'f')
0 ignored issues
show
Deprecated Code introduced by
The function db_select() has been deprecated: as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call select() on it. For example, $injected_database->select($table, $alias, $options); ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
  $query = /** @scrutinizer ignore-deprecated */ db_select('file_managed', 'f')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
65
      ->condition('filemime', $mimetypes , 'IN')
66
      ->condition('uri', $regex, 'REGEXP');
67
  $total_count = $query->countQuery()->execute()->fetchField();
68
69
  // Select the files to have derivatives created..
70
  $files = $query->fields('f', array('fid', 'filename', 'uri'))
71
      ->execute()
72
      ->fetchAll();
73
74
  $message = \Drupal::translation()->formatPlural(
75
      $total_count,
76
      '1 entry is to have its image derivatives created.',
77
      '@total_count entries are to have their image derivatives created.',
78
      array('@total_count' => $total_count)
79
  );
80
  $operations[] = array('drush_log', array($message, 'success'));
81
82
  foreach (\Drupal\image\Entity\ImageStyle::loadMultiple() as $name => $style) {
83
    if (!empty($excludes) && in_array($style->getName(), $excludes)) {
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Drupal\Core\Entity\EntityInterface. It seems like you code against a sub-type of Drupal\Core\Entity\EntityInterface such as Drupal\openapi_test\Enti...nApiTestEntityInterface or Drupal\media\MediaInterface or Drupal\taxonomy\TermInterface or Drupal\basic_data\Entity\BasicDataInterface or Drupal\entity_test\Entity\EntityTest or Drupal\openapi_test\Entity\OpenApiTestEntity or Drupal\basic_data\Entity\BasicData or Drupal\media\Entity\Media or Drupal\taxonomy\Entity\Term or Drupal\media\MediaInterface or Drupal\taxonomy\TermInterface or Drupal\entity_test\Entity\EntityTestConstraints or Drupal\basic_data\Entity\BasicDataInterface or Drupal\entity_test\Entity\EntityTestMulChanged or Drupal\media\Entity\Media or Drupal\taxonomy\Entity\Term or Drupal\media\MediaInterface or Drupal\taxonomy\TermInterface or Drupal\entity_test\Entit...vChangedWithRevisionLog or Drupal\media\Entity\Media or Drupal\taxonomy\Entity\Term or Drupal\openapi_test\Enti...nApiTestEntityInterface or Drupal\media\MediaInterface or Drupal\taxonomy\TermInterface or Drupal\basic_data\Entity\BasicDataInterface or Drupal\entity_test\Entity\EntityTest or Drupal\openapi_test\Entity\OpenApiTestEntity or Drupal\basic_data\Entity\BasicData or Drupal\media\Entity\Media or Drupal\taxonomy\Entity\Term or Drupal\entity_test\Entity\EntityTest or Drupal\openapi_test\Entity\OpenApiTestEntity or Drupal\basic_data\Entity\BasicData or Drupal\media\Entity\Media or Drupal\taxonomy\Entity\Term or Drupal\entity_browser\Entity\EntityBrowser or Drupal\facets_summary\Entity\FacetsSummary or Drupal\Core\Field\FieldConfigBase or Drupal\field\Entity\FieldStorageConfig or Drupal\image\Entity\ImageStyle or Drupal\facets\Entity\Facet or Drupal\language\Entity\ConfigurableLanguage or Drupal\facets\Entity\FacetSource or Drupal\entity_test\Entity\EntityTestMulRevPub or Drupal\media\MediaInterface or Drupal\taxonomy\TermInterface or Drupal\media\Entity\Media or Drupal\taxonomy\Entity\Term or Drupal\openapi_test\Enti...nApiTestEntityInterface or Drupal\media\MediaInterface or Drupal\taxonomy\TermInterface or Drupal\basic_data\Entity\BasicDataInterface or Drupal\entity_test\Entity\EntityTest or Drupal\openapi_test\Entity\OpenApiTestEntity or Drupal\basic_data\Entity\BasicData or Drupal\media\Entity\Media or Drupal\taxonomy\Entity\Term or Drupal\openapi_test\Enti...nApiTestEntityInterface or Drupal\media\MediaInterface or Drupal\taxonomy\TermInterface or Drupal\basic_data\Entity\BasicDataInterface or Drupal\entity_test\Entity\EntityTest or Drupal\openapi_test\Entity\OpenApiTestEntity or Drupal\basic_data\Entity\BasicData or Drupal\media\Entity\Media or Drupal\taxonomy\Entity\Term or Drupal\language\ConfigurableLanguageInterface or Drupal\facets\FacetSourceInterface or Drupal\facets\FacetInterface or Drupal\field\FieldStorageConfigInterface or Drupal\field\FieldConfigInterface or Drupal\entity_browser\EntityBrowserInterface or Drupal\image\ImageStyleInterface or Drupal\facets_summary\FacetsSummaryInterface or Drupal\Core\Field\FieldConfigInterface or Drupal\views_ui\ViewUI or Drupal\entity_browser\Entity\EntityBrowser or Drupal\facets_summary\Entity\FacetsSummary or Drupal\Core\Field\FieldConfigBase or Drupal\field\Entity\FieldStorageConfig or Drupal\image\Entity\ImageStyle or Drupal\facets\Entity\Facet or Drupal\language\Entity\ConfigurableLanguage or Drupal\facets\Entity\FacetSource or Drupal\language\ConfigurableLanguageInterface or Drupal\facets\FacetSourceInterface or Drupal\facets\FacetInterface or Drupal\field\FieldStorageConfigInterface or Drupal\field\FieldConfigInterface or Drupal\entity_browser\EntityBrowserInterface or Drupal\image\ImageStyleInterface or Drupal\facets_summary\FacetsSummaryInterface or Drupal\Core\Field\FieldConfigInterface or Drupal\views_ui\ViewUI or Drupal\entity_browser\Entity\EntityBrowser or Drupal\facets_summary\Entity\FacetsSummary or Drupal\Core\Field\FieldConfigBase or Drupal\field\Entity\FieldStorageConfig or Drupal\image\Entity\ImageStyle or Drupal\facets\Entity\Facet or Drupal\language\Entity\ConfigurableLanguage or Drupal\facets\Entity\FacetSource or Drupal\entity_browser\Entity\EntityBrowser or Drupal\image\Entity\ImageStyle. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
    if (!empty($excludes) && in_array($style->/** @scrutinizer ignore-call */ getName(), $excludes)) {
Loading history...
84
      $operations[] = array('drush_log', array('Excluding ' . $name, 'success'));
85
      continue;
86
    }
87
88
    if (empty($includes[0]) || in_array($style->getName(), $includes)) {
89
      $operations[] = array('drush_log', array('Processing ' . $name, 'success'));
90
      $count = 1;
91
      $last_progress = 0;
92
      foreach ($files as $file) {
93
        $derivative_uri = $style->buildUri($file->uri);
0 ignored issues
show
Bug introduced by
The method buildUri() does not exist on Drupal\Core\Entity\EntityInterface. It seems like you code against a sub-type of Drupal\Core\Entity\EntityInterface such as Drupal\aggregator\Entity\Item or Drupal\aggregator\Entity\Item or Drupal\aggregator\Entity\Item or Drupal\aggregator\Entity\Item or Drupal\aggregator\Entity\Item or Drupal\image\Entity\ImageStyle or Drupal\aggregator\Entity\Item or Drupal\aggregator\Entity\Item or Drupal\aggregator\Entity\Item or Drupal\aggregator\Entity\Item or Drupal\image\ImageStyleInterface or Drupal\views_ui\ViewUI or Drupal\image\Entity\ImageStyle or Drupal\image\ImageStyleInterface or Drupal\views_ui\ViewUI or Drupal\image\Entity\ImageStyle or Drupal\image\Entity\ImageStyle. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        /** @scrutinizer ignore-call */ 
94
        $derivative_uri = $style->buildUri($file->uri);
Loading history...
94
95
        if (!file_exists($derivative_uri)) {
96
          $operations[] = array('df_tools_image_batch_process', array($style, $file->uri, $derivative_uri));
97
        }
98
        $progress = round(($count / $total_count) * 100);
99
        if (($progress % 25 === 0 || $progress % 7 === 0) && $progress != $last_progress) {
100
          // If we're in the command line, print the percentage, otherwise hide it
101
          if (PHP_SAPI === 'cli') {
102
            $operations[] = array('drush_print', array('...' . $progress . '%', 0, NULL, ($progress == 100)));
103
          }
104
        $last_progress = $progress;
105
        }
106
      $count++;
107
      }
108
    }
109
  }
110
  return $operations;
111
}
112
113
function df_tools_image_batch_process($style, $uri, $derivative_uri) {
114
  $style->createDerivative($uri, $derivative_uri);
115
}
116