Skip to content

ClassConstantUsageCorrectness ​

error on by default

Group: Probable bugs · PhpStorm name: ClassConstantUsageCorrectnessInspection

Foo::class is resolved at compile time from the name as written, so a class name typed with the wrong letter case (or imported with the wrong case) produces a string that differs from the real class name. That string then breaks case-sensitive comparisons, array keys, container ids and autoloaders on case-sensitive file systems.

Example ​

php
<?php

namespace Catalog {
    class Product {}
    class Variant {}
}

namespace Storefront {
    use Catalog\product;
    use Catalog\Variant as Option;

    return [
        Product::class,
        OPTION::class,
        Option::class,
        Widgets\banner::class,
        Widgets\Banner::class,
    ];
}

namespace Storefront\Widgets {
    use Catalog\Product;

    class Banner {}

    return [
        Product::class,
        product::class,
        banner::class,
    ];
}

namespace Checkout {
    use Storefront\Widgets as W;
    use Storefront\Widgets\Banner as Promo;

    class Cart
    {
        public function names()
        {
            return [
                W\Banner::class,
                W\BANNER::class,
                \arrayobject::class,
                \ArrayObject::class,
                Promo::class,
                self::class,
                static::class,
            ];
        }
    }
}

Reported:

  • line 13: Letter case of the class name differs from its declaration; ::class will return the wrong string.
  • line 14: Letter case of the class name differs from its declaration; ::class will return the wrong string.
  • line 16: Letter case of the class name differs from its declaration; ::class will return the wrong string.
  • line 28: Letter case of the class name differs from its declaration; ::class will return the wrong string.
  • line 43: Letter case of the class name differs from its declaration; ::class will return the wrong string.
  • line 44: Letter case of the class name differs from its declaration; ::class will return the wrong string.

Configure ​

In custos.json:

json
{
  "rules": {
    "ClassConstantUsageCorrectness": {
      "enabled": false
    }
  }
}

Suppress ​

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

php
// @custos-ignore ClassConstantUsageCorrectness

/**
 * @noinspection ClassConstantUsageCorrectnessInspection
 */

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