├── .gitignore ├── infection.json.dist ├── tests ├── TestCase.php ├── RemoveTest.php └── AddTest.php ├── src ├── Strategy │ ├── StrategyInterface.php │ ├── AbstractStrategy.php │ ├── Remove.php │ └── Add.php ├── FileSystemReplacer.php └── Command.php ├── .travis.yml ├── bin ├── declare_strict_types.php └── declare_strict_types ├── phpunit.xml.dist ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | .idea 3 | phpunit.xml 4 | infection.json 5 | .composer.lock -------------------------------------------------------------------------------- /infection.json.dist: -------------------------------------------------------------------------------- 1 | { 2 | "timeout": 10, 3 | "source": { 4 | "directories": [ 5 | "bin", 6 | "src" 7 | ] 8 | } 9 | } -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /src/Strategy/StrategyInterface.php: -------------------------------------------------------------------------------- 1 | isAffected; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /bin/declare_strict_types.php: -------------------------------------------------------------------------------- 1 | add($command); 9 | $application->setDefaultCommand($command->getName(), true); 10 | $application->run(); 11 | -------------------------------------------------------------------------------- /src/Strategy/Remove.php: -------------------------------------------------------------------------------- 1 | isAffected 17 | ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /bin/declare_strict_types: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | >> */ 16 | } 17 | 18 | require __DIR__ . '/declare_strict_types.php'; -------------------------------------------------------------------------------- /src/Strategy/Add.php: -------------------------------------------------------------------------------- 1 | isAffected 24 | ); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | tests 12 | 13 | 14 | 15 | 16 | src 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wujunze/declare-strict-easy", 3 | "description": "PHP7 tool for easy add/remove \"declare(strict_types=1)\"", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "wujunze", 9 | "email": "itwujunze@gamil.com" 10 | }, 11 | { 12 | "name": "dypa", 13 | "email": "dypa@bk.ru" 14 | } 15 | ], 16 | "minimum-stability": "stable", 17 | "require": { 18 | "symfony/console": "^4.1", 19 | "symfony/finder": "^4.1" 20 | }, 21 | "require-dev": { 22 | "phpunit/phpunit": "^7.3" 23 | }, 24 | "bin": [ 25 | "bin/declare_strict_types" 26 | ], 27 | "scripts": { 28 | 29 | }, 30 | "autoload": { 31 | "psr-4": { 32 | "Dypa\\DeclareStrictTypes\\": "src/" 33 | } 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "Tests\\": "tests/" 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 dypa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP7 tool for easy add/remove "declare(strict_types=1)" 2 | 3 | [![Build Status](https://travis-ci.org/wujunze/declare-strict-easy.svg?branch=master)](https://travis-ci.org/wujunze/declare-strict-easy) 4 | [![Latest Stable Version](https://poser.pugx.org/wujunze/declare-strict-easy/v/stable.png)](//packagist.org/packages/wujunze/declare-strict-easy) 5 | [![Latest Unstable Version](https://poser.pugx.org/wujunze/declare-strict-easy/v/unstable.png)](//packagist.org/packages/wujunze/declare-strict-easy) 6 | [![Total Downloads](https://poser.pugx.org/wujunze/declare-strict-easy/downloads.png)](//packagist.org/packages/wujunze/declare-strict-easy) 7 | 8 | Enable strict typing in your project with one command. Based on PCRE and supports PSR-2. 9 | 10 | WARNING: before run command ensure that you have backup of your files!!! 11 | 12 | ## Base on [declare-strict-types](https://github.com/dypa/declare-strict-types) 13 | ## Thanks to [declare-strict-types](https://github.com/dypa/declare-strict-types) 14 | 15 | ## Usage 16 | 17 | Install via composer 18 | 19 | `composer require --dev wujunze/declare-strict-easy` 20 | 21 | Run command to add "declare(strict_types=1)" in all files in specified folders 22 | 23 | `bin/declare_strict_types add --exclude=bar/baz/bah foo/directory bar/baz` 24 | 25 | Also supports remove mode 26 | 27 | `bin/declare_strict_types remove foo/directory` 28 | 29 | ## PS 30 | 31 | You may prefer [PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) with "declare_strict_types" rule! 32 | -------------------------------------------------------------------------------- /src/FileSystemReplacer.php: -------------------------------------------------------------------------------- 1 | includeDirectories = $includeDirectories; 20 | $this->excludeDirectories = $excludeDirectories; 21 | } 22 | 23 | private function getFilesList(): \Iterator 24 | { 25 | $finder = new Finder(); 26 | $finder->in($this->includeDirectories); 27 | $finder->exclude($this->excludeDirectories); 28 | $finder->files(); 29 | $finder->followLinks(); 30 | $finder->name('*.php'); 31 | 32 | return $finder->getIterator(); 33 | } 34 | 35 | private function fileIterator(\Iterator $iterator, StrategyInterface $callback) 36 | { 37 | foreach ($iterator as $file) { 38 | /** @var $file \SplFileInfo */ 39 | $filePath = $file->getRealPath(); 40 | file_put_contents( 41 | $filePath, 42 | $callback(file_get_contents($filePath)) 43 | ); 44 | if ($callback->getIsAffected()) { 45 | $this->affectedFiles[] = $filePath; 46 | } 47 | } 48 | } 49 | 50 | public function replace(StrategyInterface $callback) 51 | { 52 | $this->fileIterator($this->getFilesList(), $callback); 53 | } 54 | 55 | public function getAffectedFiles():array 56 | { 57 | return $this->affectedFiles; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /tests/RemoveTest.php: -------------------------------------------------------------------------------- 1 | strategy = new Remove(); 14 | } 15 | 16 | /** 17 | * @covers Dypa\DeclareStrictTypes\Strategy\Remove::__invoke 18 | */ 19 | public function testNonPhp() 20 | { 21 | $this->assertEquals( 22 | 'some text', 23 | ($this->strategy)('some text') 24 | ); 25 | } 26 | 27 | /** 28 | * @covers Dypa\DeclareStrictTypes\Strategy\Remove::__invoke 29 | */ 30 | public function testEmptyPhp() 31 | { 32 | $this->assertEquals( 33 | "strategy)("assertEquals( 44 | "strategy)("assertEquals( 55 | "\n2\nstrategy)("\n2\nassertEquals( 66 | "strategy)("strategy = new Add(); 14 | } 15 | 16 | /** 17 | * @covers Dypa\DeclareStrictTypes\Strategy\Add::__invoke 18 | */ 19 | public function testNonPhp() 20 | { 21 | $this->assertEquals( 22 | 'some text', 23 | ($this->strategy)('some text') 24 | ); 25 | } 26 | 27 | /** 28 | * @covers Dypa\DeclareStrictTypes\Strategy\Add::__invoke 29 | */ 30 | public function testEmptyPhp() 31 | { 32 | $this->assertEquals( 33 | "strategy)("assertEquals( 44 | "strategy)("assertEquals( 55 | "\n2\nstrategy)("\n2\nassertEquals( 66 | "strategy)("assertEquals( 77 | "strategy)("assertEquals( 88 | "strategy)("setName('declare_strict_types'); 25 | $this->setDescription(self::DESCRIPTION); 26 | $this->setHelp('TODO'); 27 | 28 | $this->addArgument( 29 | 'mode', 30 | InputArgument::REQUIRED, 31 | 'Add/remove strict mode' 32 | ); 33 | 34 | $this->addArgument( 35 | 'include', 36 | InputArgument::IS_ARRAY | InputArgument::REQUIRED, 37 | 'Which directories must be changed?' 38 | ); 39 | 40 | $this->addOption( 41 | 'exclude', 42 | 'e', 43 | InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 44 | 'Which directories must be excluded?' 45 | ); 46 | } 47 | 48 | protected function execute(InputInterface $input, OutputInterface $output) 49 | { 50 | $mode = $input->getArgument('mode'); 51 | $includeDirectories = $input->getArgument('include'); 52 | $excludeDirectories = $input->getOption('exclude'); 53 | 54 | if (!in_array($mode, [self::MODE_ADD, self::MODE_REMOVE])) { 55 | throw new \InvalidArgumentException('You must chose between add and remove mode'); 56 | } 57 | if (count($includeDirectories) == 0) { 58 | throw new \InvalidArgumentException('You must provide at least one folder or file'); 59 | } 60 | 61 | if ($mode == self::MODE_ADD) { 62 | $replaceStrategy = new Add(); 63 | } elseif ($mode == self::MODE_REMOVE) { 64 | $replaceStrategy = new Remove(); 65 | } 66 | 67 | $replacer = new FileSystemReplacer($includeDirectories, $excludeDirectories); 68 | $replacer->replace($replaceStrategy); 69 | 70 | $output->writeln($replacer->getAffectedFiles(), OutputInterface::VERBOSITY_VERBOSE); 71 | 72 | $output->writeln([ 73 | 'Number of changed files: '.count($replacer->getAffectedFiles()), 74 | 'All done, do not forget run your tests!', 75 | ]); 76 | } 77 | } 78 | --------------------------------------------------------------------------------