Skip to content

ParameterDefaultValueIsNotNull ​

info off by default

Group: Code style · PhpStorm name: ParameterDefaultValueIsNotNullInspection

Optional parameters that may be "absent" are best expressed with a null default (and a nullable type) rather than a sentinel such as 0, '' or []. This opt-in style rule flags non-null defaults where null could be used.

Example ​

php
<?php
class Shape {
    public function scale(
        $factor,
        $min = 1,
        $max = null,
        $tags = ['a']
    ) {}

    private function tint($hue = 'red') {}
}

class Circle extends Shape {
    public function scale($factor, $min = 1, $max = null, $tags = ['a']) {}   // overrides parent

    private function tint($hue = 'blue') {}  // parent's is private
}

function pad(?int $width = 8, int $fill = 0) {}
function wrap(string|null $glue = ',', string $edge = '') {}
/** @param int|null $n */
function count_up($n = 10, $m = NULL) {}
$cb = function ($limit = 3) {};

Reported:

  • line 5: Prefer null as the default value for this parameter.
  • line 7: Prefer null as the default value for this parameter.
  • line 10: Prefer null as the default value for this parameter.
  • line 16: Prefer null as the default value for this parameter.
  • line 19: Prefer null as the default value for this parameter.
  • line 20: Prefer null as the default value for this parameter.
  • line 22: Prefer null as the default value for this parameter.
  • line 23: Prefer null as the default value for this parameter.

Configure ​

In custos.json:

json
{
  "rules": {
    "ParameterDefaultValueIsNotNull": {
      "enabled": true
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore ParameterDefaultValueIsNotNull

/**
 * @noinspection ParameterDefaultValueIsNotNullInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.