Fork me on GitHub

Rule declare_strict_types

Force a strict types mode in all files.

Warnings

This rule is RISKY

Enabling strict types will stop non strict code from working.

This rule is CONFIGURABLE

You can configure this rule using the following options: preserve_existing_declaration, strategy.

Configuration

preserve_existing_declaration

Warning

This option is deprecated and will be removed in the next major version. Use strategy to configure behaviour.

Whether existing strict_types=? should be preserved and not overridden.

Allowed types: bool

Default value: false

strategy

Whether existing strict_types=? should be enforced, removed (effectively turning strict mode off and using the engine into default type coercion mode), or added when missing.

Allowed values: 'add_when_missing', 'enforce' and 'remove'

Default value: 'enforce'

Examples

Example #1

Default configuration.

--- Original
+++ New
-<?php
+<?php declare(strict_types=1);
\ No newline at end of file

Example #2

With configuration: ['strategy' => 'enforce'].

--- Original
+++ New
 <?php
-declare(Strict_Types=0);
+declare(strict_types=1);

Example #3

With configuration: ['strategy' => 'add_when_missing'].

--- Original
+++ New
 <?php
-declare(Strict_Types=0);
+declare(strict_types=0);

Example #4

With configuration: ['strategy' => 'remove'].

--- Original
+++ New
 <?php
-declare(strict_types=1);
+

Example #5

With configuration: ['strategy' => 'remove'].

--- Original
+++ New
 <?php
-declare(strict_types=1, ticks=1);
+declare(ticks=1);

Example #6

With configuration: ['preserve_existing_declaration' => false].

--- Original
+++ New
 <?php
-declare(Strict_Types=0);
+declare(strict_types=1);

Example #7

With configuration: ['preserve_existing_declaration' => true].

--- Original
+++ New
 <?php
-declare(Strict_Types=0);
+declare(strict_types=0);

Rule sets

The rule is part of the following rule sets:

References

The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.