diff --git a/.gitignore b/.gitignore index eaa5bedc..10170914 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ -/vendor/ -/build/ -/doc/doc-en/ +/generator/vendor/ +/generator/build/ +/generator/doc/doc-en/ +/generator/doc/entities/generated.ent +/composer.lock \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 962786eb..3d497742 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,28 +8,31 @@ matrix: cache: directories: - - doc/doc-en + - generator/doc/doc-en - vendor + - generator/vendor - $HOME/.composer before_script: -- composer install --no-interaction -- mkdir -p build/logs +- cd generator && composer install --no-interaction && cd .. - | - if [ ! -d "doc/doc-en/en" ]; then - cd doc + if [ ! -d "generator/doc/doc-en/en" ]; then + cd generator/doc svn co https://svn.php.net/repository/phpdoc/modules/doc-en doc-en - cd .. + cd ../.. else - cd doc/doc-en + cd generator/doc/doc-en svn update - cd ../.. + cd ../../.. fi +- composer update script: -- "./vendor/bin/phpunit" -#- "./vendor/bin/composer-require-checker --config-file=composer-require-checker.json" +- cd generator && ./vendor/bin/phpunit && cd .. +- cd generator && composer cs-check && cd .. +- cd generator && composer phpstan && cd .. - composer cs-check - composer phpstan + after_script: - travis_retry php vendor/bin/php-coveralls diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c022a19e..c87d9229 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,31 +7,25 @@ Safe-PHP code is generated automatically from the PHP doc. The first step is to download the PHP documentation project locally. You will need Subversion (svn) installed on your computer. +```bash +$ cd generator/doc +$ svn co https://svn.php.net/repository/phpdoc/modules/doc-en doc-en +$ cd ../.. ``` -cd doc -svn co https://svn.php.net/repository/phpdoc/modules/doc-en doc-en -cd .. -``` - -Generating the documentation is a 2 pass process. -### First pass: generating the function list +At any point, if you want to update the documentation to the latest version, you can use: ```bash -php ./parse.php +$ cd generator/doc/doc-en +$ svn update ``` -The first pass is used to find all the functions in the documentation and to put them in a CSV file called `generated/functions.csv` -This CSV file is then **manually edited** to cope for the specific cases of some functions (for instance, the cURL exception -messages can be fetched from `curl_strerror`). +### Generating the functions -### Second pass: generating the function list +Generating the functions can be done with a simple command line. ```bash -php ./generate.php +$ cd generator +$ php ./safe.php generate ``` - -The second pass uses the `generated/functions.csv` and the downloaded documentation to create the functions wrapper file -in `generated/lib.php`. - diff --git a/composer.json b/composer.json index 78268f20..daaabb97 100644 --- a/composer.json +++ b/composer.json @@ -1,30 +1,19 @@ { "name": "thecodingmachine/safe", - "description": "Create a list of functional PHP functions which return false on error", + "description": "PHP core functions that throw exceptions instead of returning FALSE on error", "autoload": { "psr-4": { "Safe\\": ["lib/", "generated/"] } }, - "autoload-dev": { - "psr-4": { - "Safe\\": "src/" - } - }, "require-dev": { "php": ">=7.1", - "ext-simplexml": "*", - "phpunit/phpunit": "^7", "phpstan/phpstan": "^0.10.3", "thecodingmachine/phpstan-strict-rules": "^0.10.3", - "phpoffice/phpspreadsheet": "^1.4", - "ext-json": "^1.5", - "symfony/console": "^4.1.4", - "squizlabs/php_codesniffer": "^3.2", - "php-coveralls/php-coveralls": "^2.1" + "squizlabs/php_codesniffer": "^3.2" }, "scripts": { - "phpstan": "phpstan analyse src -c phpstan.neon --level=7 --no-progress -vvv", + "phpstan": "phpstan analyse lib -c phpstan.neon --level=7 --no-progress -vvv", "cs-fix": "phpcbf", "cs-check": "phpcs" }, diff --git a/doc/entities/.gitkeep b/doc/entities/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/generator/composer.json b/generator/composer.json new file mode 100644 index 00000000..f25c44d4 --- /dev/null +++ b/generator/composer.json @@ -0,0 +1,31 @@ +{ + "name": "thecodingmachine/safe-generator", + "description": "Generated the files for thecodingmachine/safe", + "autoload-dev": { + "psr-4": { + "Safe\\": "src/" + } + }, + "require-dev": { + "php": ">=7.1", + "ext-simplexml": "*", + "phpunit/phpunit": "^7", + "phpstan/phpstan": "^0.10.3", + "thecodingmachine/phpstan-strict-rules": "^0.10.3", + "phpoffice/phpspreadsheet": "^1.4", + "ext-json": "^1.5", + "symfony/console": "^4.1.4", + "squizlabs/php_codesniffer": "^3.2", + "php-coveralls/php-coveralls": "^2.1" + }, + "scripts": { + "phpstan": "phpstan analyse src -c phpstan.neon --level=7 --no-progress -vvv", + "cs-fix": "phpcbf", + "cs-check": "phpcs" + }, + "extra": { + "branch-alias": { + "dev-master": "0.1-dev" + } + } +} diff --git a/composer.lock b/generator/composer.lock similarity index 99% rename from composer.lock rename to generator/composer.lock index 6ab48e8e..f87a86df 100644 --- a/composer.lock +++ b/generator/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "33d3dc0aab22ea86384a160e88b47029", + "content-hash": "756f6c8abb65e6f20ebd702be6aa819c", "packages": [], "packages-dev": [ { diff --git a/doc/.gitkeep b/generator/doc/entities/.gitkeep similarity index 100% rename from doc/.gitkeep rename to generator/doc/entities/.gitkeep diff --git a/generator/phpcs.xml.dist b/generator/phpcs.xml.dist new file mode 100644 index 00000000..480486b4 --- /dev/null +++ b/generator/phpcs.xml.dist @@ -0,0 +1,21 @@ + + + Expressive Skeleton coding standard + + + + + + + + + + src + + + + + + + + \ No newline at end of file diff --git a/generator/phpstan.neon b/generator/phpstan.neon new file mode 100644 index 00000000..323181fc --- /dev/null +++ b/generator/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + ignoreErrors: + - "#subject of function str_replace expects array|string, string|true given#" + - "#createExceptionFile\\(\\) expects string, int|string given#" +includes: + - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon \ No newline at end of file diff --git a/phpunit.xml.dist b/generator/phpunit.xml.dist similarity index 100% rename from phpunit.xml.dist rename to generator/phpunit.xml.dist diff --git a/safe.php b/generator/safe.php similarity index 100% rename from safe.php rename to generator/safe.php diff --git a/generator/src/ComposerJsonEditor.php b/generator/src/ComposerJsonEditor.php new file mode 100644 index 00000000..f6f1e544 --- /dev/null +++ b/generator/src/ComposerJsonEditor.php @@ -0,0 +1,29 @@ +generateXlsFile($protoFunctions, __DIR__ . '/../generated/lib.xls'); - $fileCreator->generatePhpFile($functions, __DIR__ . '/../generated/'); - $fileCreator->generateFunctionsList($functions, __DIR__ . '/../generated/functionsList.php'); + $fileCreator->generatePhpFile($functions, __DIR__ . '/../../generated/'); + $fileCreator->generateFunctionsList($functions, __DIR__ . '/../../generated/functionsList.php'); $modules = []; foreach ($functions as $function) { - $modules[$function->getModuleName()] = true; + $modules[$function->getModuleName()] = $function->getModuleName(); } foreach ($modules as $moduleName => $foo) { @@ -51,22 +51,25 @@ protected function execute(InputInterface $input, OutputInterface $output) } // Finally, let's require the generated file to check there is no error. - $files = \glob(__DIR__.'/../generated/*.php'); + $files = \glob(__DIR__.'/../../generated/*.php'); foreach ($files as $file) { require($file); } + + // Finally, let's edit the composer.json file + ComposerJsonEditor::editFiles($modules); } private function rmGenerated(): void { - $exceptions = \glob(__DIR__.'/../generated/Exceptions/*.php'); + $exceptions = \glob(__DIR__.'/../../generated/Exceptions/*.php'); foreach ($exceptions as $exception) { \unlink($exception); } - $files = \glob(__DIR__.'/../generated/*.php'); + $files = \glob(__DIR__.'/../../generated/*.php'); foreach ($files as $file) { \unlink($file); diff --git a/src/Method.php b/generator/src/Method.php similarity index 100% rename from src/Method.php rename to generator/src/Method.php diff --git a/src/Parameter.php b/generator/src/Parameter.php similarity index 100% rename from src/Parameter.php rename to generator/src/Parameter.php diff --git a/src/PhpStanFunctions/PhpStanFunction.php b/generator/src/PhpStanFunctions/PhpStanFunction.php similarity index 100% rename from src/PhpStanFunctions/PhpStanFunction.php rename to generator/src/PhpStanFunctions/PhpStanFunction.php diff --git a/src/PhpStanFunctions/PhpStanFunctionMapReader.php b/generator/src/PhpStanFunctions/PhpStanFunctionMapReader.php similarity index 100% rename from src/PhpStanFunctions/PhpStanFunctionMapReader.php rename to generator/src/PhpStanFunctions/PhpStanFunctionMapReader.php diff --git a/src/PhpStanFunctions/PhpStanParameter.php b/generator/src/PhpStanFunctions/PhpStanParameter.php similarity index 100% rename from src/PhpStanFunctions/PhpStanParameter.php rename to generator/src/PhpStanFunctions/PhpStanParameter.php diff --git a/src/Scanner.php b/generator/src/Scanner.php similarity index 100% rename from src/Scanner.php rename to generator/src/Scanner.php diff --git a/src/WritePhpFunction.php b/generator/src/WritePhpFunction.php similarity index 100% rename from src/WritePhpFunction.php rename to generator/src/WritePhpFunction.php diff --git a/tests/DocPageTest.php b/generator/tests/DocPageTest.php similarity index 100% rename from tests/DocPageTest.php rename to generator/tests/DocPageTest.php diff --git a/tests/MethodTest.php b/generator/tests/MethodTest.php similarity index 100% rename from tests/MethodTest.php rename to generator/tests/MethodTest.php diff --git a/tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php b/generator/tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php similarity index 100% rename from tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php rename to generator/tests/PhpStanFunctions/PhpStanFunctionMapReaderTest.php diff --git a/lib/Exceptions/AbstractSafeException.php b/lib/Exceptions/AbstractSafeException.php index 11f03e5e..2e36f09c 100644 --- a/lib/Exceptions/AbstractSafeException.php +++ b/lib/Exceptions/AbstractSafeException.php @@ -3,7 +3,6 @@ namespace Safe\Exceptions; - abstract class AbstractSafeException extends \ErrorException implements SafeExceptionInterface { public static function createFromPhpError(): self diff --git a/lib/Exceptions/CurlException.php b/lib/Exceptions/CurlException.php index 85847438..8920fd21 100644 --- a/lib/Exceptions/CurlException.php +++ b/lib/Exceptions/CurlException.php @@ -3,7 +3,6 @@ namespace Safe\Exceptions; - class CurlException extends AbstractSafeException { /** diff --git a/lib/Exceptions/JsonException.php b/lib/Exceptions/JsonException.php index 183efdfa..dc83a162 100644 --- a/lib/Exceptions/JsonException.php +++ b/lib/Exceptions/JsonException.php @@ -3,7 +3,6 @@ namespace Safe\Exceptions; - class JsonException extends \Exception implements SafeExceptionInterface { public static function createFromPhpError(): self diff --git a/lib/Exceptions/SafeExceptionInterface.php b/lib/Exceptions/SafeExceptionInterface.php index 89da6c60..fbea6ad2 100644 --- a/lib/Exceptions/SafeExceptionInterface.php +++ b/lib/Exceptions/SafeExceptionInterface.php @@ -3,8 +3,7 @@ namespace Safe\Exceptions; - interface SafeExceptionInterface extends \Throwable { -} \ No newline at end of file +} diff --git a/parse.php b/parse.php deleted file mode 100755 index 93b09ac5..00000000 --- a/parse.php +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env php -detectFalsyFunction()) { - $functionObjects = $docPage->getMethodSynopsis(); - foreach ($functionObjects as $functionObject) { - $method = new Method($functionObject); - $writePhpFunction = new WritePhpFunction($method); - $phpFunctions[] = $writePhpFunction->getPhpPrototypeFunction(); - print_r($functionObject->methodparam[2]->initializer); - } - } -/* $docPage = new DocPage(__DIR__ . '/doc/doc-en/en/reference/apc/functions/apc-dec.xml'); - if ($docPage->detectFalsyFunction()) { - $functionObjects = $docPage->getMethodSynopsis(); - foreach ($functionObjects as $functionObject) {*/ - /* $method = new Method($functionObject); - $writePhpFunction = new WritePhpFunction($method); - $phpFunctions[] = $writePhpFunction->getPhpPrototypeFunction();*/ -/* print_r($functionObject); - } - }*/ - //print_r($method); -} - -function multiPath() -{ - $pathResearcher = new PathResearcher(__DIR__ . '/doc/doc-en/en/reference/*'); - $paths = $pathResearcher->getPaths(); - foreach ($paths as $path) { - $docPage = new DocPage($path); - if ($docPage->detectFalsyFunction()) { - $functionObjects = $docPage->getMethodSynopsis(); - foreach ($functionObjects as $functionObject) { - $method = new Method($functionObject); - $writePhpFunction = new WritePhpFunction($method); - $protoFunctions[] = $writePhpFunction->getPhpPrototypeFunction(); - $phpFunctions[] = $writePhpFunction->getPhpFunctionalFunction(); - } - } - } - $fileCreator = new FileCreator(); - $fileCreator->generateXlsFile($protoFunctions, __DIR__ . '/generated/lib.xls'); - $fileCreator->generatePhpFile($phpFunctions, __DIR__ . '/generated/lib.php'); -} - - -//simplePath(); -multiPath(); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 480486b4..9febe85d 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -10,7 +10,7 @@ - src + lib diff --git a/phpstan.neon b/phpstan.neon index 323181fc..bf8cad2d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,4 @@ parameters: ignoreErrors: - - "#subject of function str_replace expects array|string, string|true given#" - - "#createExceptionFile\\(\\) expects string, int|string given#" includes: - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon \ No newline at end of file