Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/vendor/
/build/
/doc/doc-en/
/generator/vendor/
/generator/build/
/generator/doc/doc-en/
/generator/doc/entities/generated.ent
/composer.lock
23 changes: 13 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 11 additions & 17 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

17 changes: 3 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
Empty file removed doc/entities/.gitkeep
Empty file.
31 changes: 31 additions & 0 deletions generator/composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
2 changes: 1 addition & 1 deletion composer.lock → generator/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
21 changes: 21 additions & 0 deletions generator/phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="Expressive Skeleton coding standard">
<description>Expressive Skeleton coding standard</description>

<!-- display progress -->
<arg value="p"/>
<arg name="colors"/>

<!-- inherit rules from: -->
<rule ref="PSR2"/>

<!-- Paths to check -->
<file>src</file>

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="300"/>
<property name="absoluteLineLimit" value="500"/>
</properties>
</rule>
</ruleset>
6 changes: 6 additions & 0 deletions generator/phpstan.neon
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions generator/src/ComposerJsonEditor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


namespace Safe;

/**
* This class will edit the main composer.json file to add the list of files generated from modules.
*/
class ComposerJsonEditor
{
/**
* @param string[] $modules A list of modules
*/
public static function editFiles(array $modules): void
{
$files = \array_map(function (string $module) {
return 'generated/'.lcfirst($module).'.php';
}, $modules);
$composerContent = file_get_contents(__DIR__.'/../../composer.json');
if ($composerContent === false) {
throw new \RuntimeException('Error while loading composer.json file for edition.');
}
$composerJson = \json_decode($composerContent, true);
$composerJson['autoload']['files'] = $files;

$newContent = json_encode($composerJson, \JSON_PRETTY_PRINT);
\file_put_contents(__DIR__.'/../../composer.json', $newContent);
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/FileCreator.php → generator/src/FileCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function createExceptionFile(string $moduleName): void
{
if (!\class_exists("Safe\\Exceptions\\{$moduleName}Exception")) {
\file_put_contents(
__DIR__.'/../generated/Exceptions/'.$moduleName.'Exception.php',
__DIR__.'/../../generated/Exceptions/'.$moduleName.'Exception.php',
<<<EOF
<?php
namespace Safe\Exceptions;
Expand Down
15 changes: 9 additions & 6 deletions src/GenerateCommand.php → generator/src/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,39 @@ protected function execute(InputInterface $input, OutputInterface $output)

$fileCreator = new FileCreator();
//$fileCreator->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) {
$fileCreator->createExceptionFile($moduleName);
}

// 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);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion lib/Exceptions/AbstractSafeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Safe\Exceptions;


abstract class AbstractSafeException extends \ErrorException implements SafeExceptionInterface
{
public static function createFromPhpError(): self
Expand Down
1 change: 0 additions & 1 deletion lib/Exceptions/CurlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Safe\Exceptions;


class CurlException extends AbstractSafeException
{
/**
Expand Down
1 change: 0 additions & 1 deletion lib/Exceptions/JsonException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Safe\Exceptions;


class JsonException extends \Exception implements SafeExceptionInterface
{
public static function createFromPhpError(): self
Expand Down
3 changes: 1 addition & 2 deletions lib/Exceptions/SafeExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

namespace Safe\Exceptions;


interface SafeExceptionInterface extends \Throwable
{

}
}
59 changes: 0 additions & 59 deletions parse.php

This file was deleted.

2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<rule ref="PSR2"/>

<!-- Paths to check -->
<file>src</file>
<file>lib</file>

<rule ref="Generic.Files.LineLength">
<properties>
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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