Immutable.php 1.2 KB

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace JetBrains\PhpStorm;
  3. use Attribute;
  4. /**
  5. * Mark a property (or all class properties in the case of a class) as immutable.
  6. * By default, an IDE highlights write accesses on such properties if they are located outside a constructor (this scope is customizable, see below).
  7. *
  8. * You can provide a custom allowed write scope by using the following values:
  9. * <ul>
  10. * <li>{@link Immutable::CONSTRUCTOR_WRITE_SCOPE}: write is allowed only in containing class constructor (default choice)</li>
  11. * <li>{@link Immutable::PRIVATE_WRITE_SCOPE}: write is allowed only in places where the property would be accessible if it had 'private' visibility modifier</li>
  12. * <li>{@link Immutable::PROTECTED_WRITE_SCOPE}: write is allowed only in places where the property would be accessible if it had 'protected' visibility modifier</li>
  13. * </ul>
  14. * @since 8.0
  15. */
  16. #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
  17. class Immutable
  18. {
  19. const CONSTRUCTOR_WRITE_SCOPE = "constructor";
  20. const PRIVATE_WRITE_SCOPE = "private";
  21. const PROTECTED_WRITE_SCOPE = "protected";
  22. public function __construct(#[ExpectedValues(valuesFromClass: Immutable::class)]
  23. $allowedWriteScope = self::CONSTRUCTOR_WRITE_SCOPE)
  24. {
  25. }
  26. }