Skip to content

Commit c7168e2

Browse files
authored
Merge pull request #16 from moufmouf/special_cases
Adding a file to handle special cases.
2 parents a9415e4 + 9f7601e commit c7168e2

File tree

7 files changed

+48
-1
lines changed

7 files changed

+48
-1
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ script:
3131
- cd generator && ./vendor/bin/phpunit && cd ..
3232
- cd generator && composer cs-check && cd ..
3333
- cd generator && composer phpstan && cd ..
34+
- composer dump-autoload
3435
- composer cs-check
3536
- composer phpstan
3637
# Now, let's regenerate all files and see if we obtain the same set of files as the ones commited:

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
"generated/yaml.php",
8989
"generated/yaz.php",
9090
"generated/zip.php",
91-
"generated/zlib.php"
91+
"generated/zlib.php",
92+
"lib/special_cases.php"
9293
]
9394
},
9495
"require": {

generated/functionsList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,4 +931,5 @@
931931
'inflate_add',
932932
'inflate_init',
933933
'zlib_decode',
934+
'json_decode',
934935
];
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* A list of functions that must be dealt with manually.
4+
* They are declared in lib/special_cases.php
5+
*/
6+
return [
7+
'json_decode'
8+
];

generator/src/ComposerJsonEditor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static function editFiles(array $modules): void
1616
$files = \array_map(function (string $module) {
1717
return 'generated/'.lcfirst($module).'.php';
1818
}, $modules);
19+
$files[] = 'lib/special_cases.php';
1920
$composerContent = file_get_contents(__DIR__.'/../../composer.json');
2021
if ($composerContent === false) {
2122
throw new \RuntimeException('Error while loading composer.json file for edition.');

generator/src/FileCreator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Safe;
44

5+
use function array_merge;
56
use Complex\Exception;
67
use PhpOffice\PhpSpreadsheet\Spreadsheet;
78
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
@@ -89,6 +90,8 @@ public function generateFunctionsList(array $functions, string $path): void
8990
$functionNames = array_map(function (Method $function) {
9091
return $function->getFunctionName();
9192
}, $functions);
93+
$specialCases = require __DIR__.'/../config/specialCasesFunctions.php';
94+
$functionNames = array_merge($functionNames, $specialCases);
9295
$stream = fopen($path, 'w');
9396
if ($stream === false) {
9497
throw new \RuntimeException('Unable to write to '.$path);

lib/special_cases.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* This file contains all the functions that could not be dealt with automatically using the code generator.
4+
* If you add a function in this list, do not forget to add it in the generator/config/speicalCasesFunctions.php
5+
*
6+
*/
7+
8+
namespace Safe;
9+
10+
use Safe\Exceptions\JsonException;
11+
12+
/**
13+
* Wrapper for json_decode that throws when an error occurs.
14+
*
15+
* @param string $json JSON data to parse
16+
* @param bool $assoc When true, returned objects will be converted
17+
* into associative arrays.
18+
* @param int $depth User specified recursion depth.
19+
* @param int $options Bitmask of JSON decode options.
20+
*
21+
* @return mixed
22+
* @throws JsonException if the JSON cannot be decoded.
23+
* @link http://www.php.net/manual/en/function.json-decode.php
24+
*/
25+
function json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
26+
{
27+
$data = \json_decode($json, $assoc, $depth, $options);
28+
if (JSON_ERROR_NONE !== json_last_error()) {
29+
throw JsonException::createFromPhpError();
30+
}
31+
return $data;
32+
}

0 commit comments

Comments
 (0)