Unverified Commit 7567d544 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3142934 by jungle, mondrake, longwave, Krzysztof Domański, DevJoJodae,...

Issue #3142934 by jungle, mondrake, longwave, Krzysztof Domański, DevJoJodae, Chi, xjm, catch, andypost: Replace \Drupal\Component\Utility\Bytes::toInt() with \Drupal\Component\Utility\Bytes::toNumber() due to return type
parent edd88adf
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -23,8 +23,28 @@ class Bytes {
   *
   * @return int
   *   An integer representation of the size in bytes.
   *
   * @deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\Component\Utility\Bytes::toNumber() instead
   *
   * @see https://www.drupal.org/node/3162663
   */
  public static function toInt($size) {
    @trigger_error('\Drupal\Component\Utility\Bytes::toInt() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\Component\Utility\Bytes::toNumber() instead. See https://www.drupal.org/node/3162663', E_USER_DEPRECATED);
    return self::toNumber($size);
  }

  /**
   * Parses a given byte size.
   *
   * @param int|float|string $size
   *   An integer, float, or string size expressed as a number of bytes with
   *   optional SI or IEC binary unit prefix (e.g. 2, 2.4, 3K, 5MB, 10G, 6GiB,
   *   8 bytes, 9mbytes).
   *
   * @return float
   *   The floating point value of the size in bytes.
   */
  public static function toNumber($size): float {
    // Remove the non-unit characters from the size.
    $unit = preg_replace('/[^bkmgtpezy]/i', '', $size);
    // Remove the non-numeric characters from the size.
+3 −3
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ public static function checkMemoryLimit($required, $memory_limit = NULL) {
    // - The memory limit is set to unlimited (-1).
    // - The memory limit is greater than or equal to the memory required for
    //   the operation.
    return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required)));
    return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toNumber($memory_limit) >= Bytes::toNumber($required)));
  }

  /**
@@ -85,11 +85,11 @@ public static function getUploadMaxSize() {

    if ($max_size < 0) {
      // Start with post_max_size.
      $max_size = Bytes::toInt(ini_get('post_max_size'));
      $max_size = Bytes::toNumber(ini_get('post_max_size'));

      // If upload_max_size is less, then reduce. Except if upload_max_size is
      // zero, which indicates no limit.
      $upload_max = Bytes::toInt(ini_get('upload_max_filesize'));
      $upload_max = Bytes::toNumber(ini_get('upload_max_filesize'));
      if ($upload_max > 0 && $upload_max < $max_size) {
        $max_size = $upload_max;
      }
+1 −1
Original line number Diff line number Diff line
@@ -398,7 +398,7 @@ function color_scheme_form_submit($form, FormStateInterface $form_state) {
    // scheme change based on a faulty memory calculation.
    $usage = memory_get_usage(TRUE);
    $memory_limit = ini_get('memory_limit');
    $size = Bytes::toInt($memory_limit);
    $size = Bytes::toNumber($memory_limit);
    if (!Environment::checkMemoryLimit($usage + $required, $memory_limit)) {
      \Drupal::messenger()->addError(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size more. Check the <a href="http://php.net/manual/ini.core.php#ini.sect.resource-limits">PHP documentation</a> for more information.', ['%size' => format_size($usage + $required - $size)]));
      return;
+1 −1
Original line number Diff line number Diff line
@@ -92,7 +92,7 @@ public function buildForm(array $form, FormStateInterface $form_state, Editor $e
    else {
      $max_dimensions = 0;
    }
    $max_filesize = min(Bytes::toInt($image_upload['max_size']), Environment::getUploadMaxSize());
    $max_filesize = min(Bytes::toNumber($image_upload['max_size']), Environment::getUploadMaxSize());
    $existing_file = isset($image_element['data-entity-uuid']) ? \Drupal::service('entity.repository')->loadEntityByUuid('file', $image_element['data-entity-uuid']) : NULL;
    $fid = $existing_file ? $existing_file->id() : NULL;

+4 −4
Original line number Diff line number Diff line
@@ -240,13 +240,13 @@ public static function validateExtensions($element, FormStateInterface $form_sta
   * Form API callback.
   *
   * Ensures that a size has been entered and that it can be parsed by
   * \Drupal\Component\Utility\Bytes::toInt().
   * \Drupal\Component\Utility\Bytes::toNumber().
   *
   * This function is assigned as an #element_validate callback in
   * fieldSettingsForm().
   */
  public static function validateMaxFilesize($element, FormStateInterface $form_state) {
    if (!empty($element['#value']) && !is_numeric(Bytes::toInt($element['#value']))) {
    if (!empty($element['#value']) && !is_numeric(Bytes::toNumber($element['#value']))) {
      $form_state->setError($element, t('The "@name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', ['@name' => $element['title']]));
    }
  }
@@ -302,9 +302,9 @@ public function getUploadValidators() {
    $settings = $this->getSettings();

    // Cap the upload size according to the PHP limit.
    $max_filesize = Bytes::toInt(Environment::getUploadMaxSize());
    $max_filesize = Bytes::toNumber(Environment::getUploadMaxSize());
    if (!empty($settings['max_filesize'])) {
      $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize']));
      $max_filesize = min($max_filesize, Bytes::toNumber($settings['max_filesize']));
    }

    // There is always a file size limit due to the PHP server limit.
Loading