diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/ContextGenerator.php | 54 | ||||
-rw-r--r-- | tools/TestGenerator.php | 51 | ||||
-rw-r--r-- | tools/sami-config.php | 4 |
3 files changed, 52 insertions, 57 deletions
diff --git a/tools/ContextGenerator.php b/tools/ContextGenerator.php index d4c5407..ba424ab 100644 --- a/tools/ContextGenerator.php +++ b/tools/ContextGenerator.php @@ -1,15 +1,14 @@ <?php +declare(strict_types=1); namespace PhpMyAdmin\SqlParser\Tools; +use Exception; + require_once __DIR__ . '/../vendor/autoload.php'; /** * Used for context generation. - * - * @category Contexts - * - * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class ContextGenerator { @@ -18,19 +17,19 @@ class ContextGenerator * * @var array */ - public static $LABELS_FLAGS = array( + public static $LABELS_FLAGS = [ '(R)' => 2, // reserved '(D)' => 8, // data type '(K)' => 16, // keyword - '(F)' => 32 // function name - ); + '(F)' => 32, // function name + ]; /** * Documentation links for each context. * * @var array */ - public static $LINKS = array( + public static $LINKS = [ 'MySql50000' => 'https://dev.mysql.com/doc/refman/5.0/en/keywords.html', 'MySql50100' => 'https://dev.mysql.com/doc/refman/5.1/en/keywords.html', 'MySql50500' => 'https://dev.mysql.com/doc/refman/5.5/en/keywords.html', @@ -40,8 +39,8 @@ class ContextGenerator 'MariaDb100000' => 'https://mariadb.com/kb/en/the-mariadb-library/reserved-words/', 'MariaDb100100' => 'https://mariadb.com/kb/en/the-mariadb-library/reserved-words/', 'MariaDb100200' => 'https://mariadb.com/kb/en/the-mariadb-library/reserved-words/', - 'MariaDb100300' => 'https://mariadb.com/kb/en/the-mariadb-library/reserved-words/' - ); + 'MariaDb100300' => 'https://mariadb.com/kb/en/the-mariadb-library/reserved-words/', + ]; /** * The template of a context. @@ -56,7 +55,6 @@ class ContextGenerator */ const TEMPLATE = '<?php' . "\n" . - '' . "\n" . '/**' . "\n" . ' * Context for %1$s.' . "\n" . ' *' . "\n" . @@ -64,6 +62,7 @@ class ContextGenerator ' *' . "\n" . ' * @see %3$s' . "\n" . ' */' . "\n" . + 'declare(strict_types=1);' . "\n" . '' . "\n" . 'namespace PhpMyAdmin\\SqlParser\\Contexts;' . "\n" . '' . "\n" . @@ -90,9 +89,9 @@ class ContextGenerator ' *' . "\n" . ' * @var array' . "\n" . ' */' . "\n" . - ' public static $KEYWORDS = array(' . "\n" . + ' public static $KEYWORDS = [' . "\n" . '%4$s' . - ' );' . "\n" . + ' ];' . "\n" . '}' . "\n"; /** @@ -124,12 +123,12 @@ class ContextGenerator */ public static function readWords(array $files) { - $words = array(); + $words = []; foreach ($files as $file) { $words = array_merge($words, file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)); } - $types = array(); + $types = []; for ($i = 0, $count = count($words); $i !== $count; ++$i) { $type = 1; @@ -162,14 +161,14 @@ class ContextGenerator } } - $ret = array(); + $ret = []; foreach ($types as $word => $type) { $len = strlen($word); if (! isset($ret[$type])) { - $ret[$type] = array(); + $ret[$type] = []; } if (! isset($ret[$type][$len])) { - $ret[$type][$len] = array(); + $ret[$type][$len] = []; } $ret[$type][$len][] = $word; } @@ -254,7 +253,7 @@ class ContextGenerator public static function formatName($name) { /* Split name and version */ - $parts = array(); + $parts = []; if (preg_match('/([^[0-9]*)([0-9]*)/', $name, $parts) === false) { return $name; } @@ -334,18 +333,18 @@ class ContextGenerator file_put_contents( $output . '/' . $class . '.php', static::generate( - array( + [ 'name' => $formattedName, 'class' => $class, 'link' => static::$LINKS[$name], 'keywords' => static::readWords( - array( + [ $directory . '_common.txt', $directory . '_functions' . $file, - $directory . $file - ) - ) - ) + $directory . $file, + ] + ), + ] ) ); } @@ -381,7 +380,6 @@ class ContextGenerator // // Input data must be in the `data` folder. // The output will be generated in the same `data` folder. -// if (count($argv) >= 3) { // Extracting directories' name from command line and trimming unnecessary // slashes at the end. @@ -390,9 +388,9 @@ if (count($argv) >= 3) { // Checking if all directories are valid. if (! is_dir($input)) { - throw new \Exception('The input directory does not exist.'); + throw new Exception('The input directory does not exist.'); } elseif (! is_dir($output)) { - throw new \Exception('The output directory does not exist.'); + throw new Exception('The output directory does not exist.'); } // Finally, building the tests. diff --git a/tools/TestGenerator.php b/tools/TestGenerator.php index 5f59f01..c5838b8 100644 --- a/tools/TestGenerator.php +++ b/tools/TestGenerator.php @@ -1,18 +1,16 @@ <?php +declare(strict_types=1); namespace PhpMyAdmin\SqlParser\Tools; require_once '../vendor/autoload.php'; +use Exception; use PhpMyAdmin\SqlParser\Lexer; use PhpMyAdmin\SqlParser\Parser; /** * Used for test generation. - * - * @category Tests - * - * @license https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+ */ class TestGenerator { @@ -39,21 +37,21 @@ class TestGenerator * * @var Parser */ - $parser = ($type === 'parser') ? new Parser($lexer->list) : null; + $parser = $type === 'parser' ? new Parser($lexer->list) : null; /** * Lexer's errors. * * @var array */ - $lexerErrors = array(); + $lexerErrors = []; /** * Parser's errors. * * @var array */ - $parserErrors = array(); + $parserErrors = []; // Both the lexer and the parser construct exception for errors. // Usually, exceptions contain a full stack trace and other details that @@ -63,37 +61,37 @@ class TestGenerator // Extracting lexer's errors. if (! empty($lexer->errors)) { foreach ($lexer->errors as $err) { - $lexerErrors[] = array( + $lexerErrors[] = [ $err->getMessage(), $err->ch, $err->pos, - $err->getCode() - ); + $err->getCode(), + ]; } - $lexer->errors = array(); + $lexer->errors = []; } // Extracting parser's errors. if (! empty($parser->errors)) { foreach ($parser->errors as $err) { - $parserErrors[] = array( + $parserErrors[] = [ $err->getMessage(), $err->token, - $err->getCode() - ); + $err->getCode(), + ]; } - $parser->errors = array(); + $parser->errors = []; } - return array( + return [ 'query' => $query, 'lexer' => $lexer, 'parser' => $parser, - 'errors' => array( + 'errors' => [ 'lexer' => $lexerErrors, 'parser' => $parserErrors, - ) - ); + ], + ]; } /** @@ -109,8 +107,8 @@ class TestGenerator public static function build($type, $input, $output, $debug = null) { // Support query types: `lexer` / `parser`. - if (! in_array($type, array('lexer', 'parser'))) { - throw new \Exception('Unknown test type (expected `lexer` or `parser`).'); + if (! in_array($type, ['lexer', 'parser'])) { + throw new Exception('Unknown test type (expected `lexer` or `parser`).'); } /** @@ -122,7 +120,7 @@ class TestGenerator // There is no point in generating a test without a query. if (empty($query)) { - throw new \Exception('No input query specified.'); + throw new Exception('No input query specified.'); } $test = static::generate($query, $type); @@ -156,7 +154,7 @@ class TestGenerator // Appending the filename to directories. $inputFile = $input . '/' . $file; $outputFile = $output . '/' . $file; - $debugFile = ($debug !== null) ? $debug . '/' . $file : null; + $debugFile = $debug !== null ? $debug . '/' . $file : null; if (is_dir($inputFile)) { // Creating required directories to maintain the structure. @@ -203,7 +201,6 @@ class TestGenerator // // Input data must be in the `../tests/data` folder. // The output will be generated in the same `../tests/data` folder. -// if (count($argv) >= 3) { // Extracting directories' name from command line and trimming unnecessary // slashes at the end. @@ -213,11 +210,11 @@ if (count($argv) >= 3) { // Checking if all directories are valid. if (! is_dir($input)) { - throw new \Exception('The input directory does not exist.'); + throw new Exception('The input directory does not exist.'); } elseif (! is_dir($output)) { - throw new \Exception('The output directory does not exist.'); + throw new Exception('The output directory does not exist.'); } elseif (($debug !== null) && (! is_dir($debug))) { - throw new \Exception('The debug directory does not exist.'); + throw new Exception('The debug directory does not exist.'); } // Finally, building the tests. diff --git a/tools/sami-config.php b/tools/sami-config.php index 39db4ed..f8dad47 100644 --- a/tools/sami-config.php +++ b/tools/sami-config.php @@ -14,9 +14,9 @@ $iterator = Finder::create() ->in("./src") ; -return new Sami($iterator, array( +return new Sami($iterator, [ "title" => "A validating SQL lexer and parser with a focus on MySQL dialect.", "build_dir" => "./doc/", "cache_dir" => "./tmp" -)); +]); |