I'm trying to add the strict type declaration declare(strict_types=1);
to all PHP class and trait files under a specific directory.
How can I get a list of files without that declaration using Linux terminal?
How can I add missing declarations using PhpStorm (inspection, replace in path + regexp)?
Example /src/Foobar.php without declaration:
<?php
/**
* class file
*/
namespace Foo\Bar;
use Other\Foo\Bar;
class Foobar {
// ..
}
Example /src/Foobar.php with declaration:
<?php
/**
* class file
*/
declare(strict_types = 1);
namespace Foo\Bar;
use Other\Foo\Bar;
class Foobar {
// ..
}
Comments and imports ("use") are optional within a class file.
What I've done:
You do not need regex for this - just use built-in "quick fix" for that.
Code | Run Inspection by Name...
Missing strict type declaration
inspection there (e.g. type strict
and select it from narrowed list) and run itFinding inspection to run:
Applying the fix (NOTE: I've run this inspection on single file only that has your sample code as content):
Alternatively -- just enable appropriate inspection (which is disabled by default) and you will be able to add such code via usual Alt + Enter (on Windows using Default keymap; or via "light bulb" icon) directly in the code:
Settings/Preferences | Editor | Inspections | PHP | Type Compatibility | Missing strict type declaration
Quick fix menu (Alt+Enter
)