From 620529521186566c8e264ca055904f7f50ebc3b9 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 7 Feb 2022 11:29:12 +0100 Subject: [PATCH 01/10] add phpstan --- composer.json | 6 +++++- phpstan.neon | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index 2351786..e9b2af6 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,10 @@ }, "require-dev": { "symplify/easy-coding-standard": "dev-main", - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^1.4" + }, + "scripts": { + "phpstan": "vendor/bin/phpstan --ansi" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..6d99280 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + level: 0 + + paths: + - src + - helpers From 9b7ba4c5c7ed92f951aabc97179ca5530e265cd2 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 7 Feb 2022 11:29:18 +0100 Subject: [PATCH 02/10] level 0 static fixes --- src/JSONDB.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/JSONDB.php b/src/JSONDB.php index 76022e5..d7a624d 100644 --- a/src/JSONDB.php +++ b/src/JSONDB.php @@ -236,7 +236,7 @@ public function insert($file, array $values) // $this->last_indexes = [ ( count( $this->content ) - 1 ) ]; $this->commit(); - // return $this->last_indexes; + return $this->last_indexes; } public function commit() @@ -328,7 +328,7 @@ private function _update() if (! array_diff_key($this->update, $content)) { $this->content[$i] = (object) array_merge($content, $this->update); } else { - throw new Exception('Update method has an off key'); + throw new \Exception('Update method has an off key'); } } else { continue; @@ -342,7 +342,7 @@ private function _update() if (! array_diff_key($this->update, $content)) { $this->content[$i] = (object) array_merge($content, $this->update); } else { - throw new Exception('Update method has an off key '); + throw new \Exception('Update method has an off key '); } } } @@ -382,8 +382,6 @@ public function trigger() /** * Flushes indexes they won't be reused on next action - * - * @return object $this */ private function flush_indexes($flush_where = false) { From ef09c36709faa890558f06f8247a4055de7c23f6 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 7 Feb 2022 11:29:44 +0100 Subject: [PATCH 03/10] level 1, 2 fixes --- phpstan.neon | 2 +- src/JSONDB.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 6d99280..3448c98 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 0 + level: 2 paths: - src diff --git a/src/JSONDB.php b/src/JSONDB.php index d7a624d..c1d4bf6 100644 --- a/src/JSONDB.php +++ b/src/JSONDB.php @@ -172,7 +172,7 @@ public function where(array $columns, $merge = 'OR') * Implements regex search on where statement * * @param string $pattern Regex pattern - * @param int $preg_grep_flags Flags for preg_grep(). See - https://www.php.net/manual/en/function.preg-match.php + * @param int $preg_match_flags Flags for preg_grep(). See - https://www.php.net/manual/en/function.preg-match.php */ public static function regex(string $pattern, int $preg_match_flags = 0): object { From 44253313725549fc9c77cd0126379aaa3ba56500 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 7 Feb 2022 11:31:13 +0100 Subject: [PATCH 04/10] level 3, 4 fixes --- helpers/dataTypes.php | 12 ++++++------ phpstan.neon | 2 +- src/JSONDB.php | 4 +--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/helpers/dataTypes.php b/helpers/dataTypes.php index c160aac..34ac14b 100644 --- a/helpers/dataTypes.php +++ b/helpers/dataTypes.php @@ -1,14 +1,14 @@ -content = $content; return true; @@ -335,7 +333,7 @@ private function _update() } } } elseif (! empty($this->where) && empty($this->last_indexes)) { - null; + return; } else { foreach ($this->content as $i => $v) { $content = (array) $this->content[$i]; From ee8a1229a24bcdd761b8524056289f5baa3d405b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 7 Feb 2022 11:08:11 +0100 Subject: [PATCH 05/10] [ci] Add first CS check --- .github/workflows/code_analysis.yaml | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/workflows/code_analysis.yaml diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml new file mode 100644 index 0000000..763f18f --- /dev/null +++ b/.github/workflows/code_analysis.yaml @@ -0,0 +1,33 @@ +name: Code Analysis + +on: + pull_request: null + push: + branches: + - master + +jobs: + code_analysis: + strategy: + fail-fast: false + matrix: + actions: + - + name: 'Coding Standard' + run: vendor/bin/ecs check + + name: ${{ matrix.actions.name }} + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + # see https://github.com/shivammathur/setup-php + - uses: shivammathur/setup-php@v2 + with: + php-version: 7.3 + coverage: none + + # composer install cache - https://github.com/ramsey/composer-install + - uses: "ramsey/composer-install@v1" + + - run: ${{ matrix.actions.run }} From 60279941422668462f568968cd5959cac01954d0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 7 Feb 2022 11:12:24 +0100 Subject: [PATCH 06/10] [composer] add handy scripts for easy cs run without configuration --- composer.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/composer.json b/composer.json index e9b2af6..98b0ff3 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,8 @@ "phpstan/phpstan": "^1.4" }, "scripts": { + "check-cs": "vendor/bin/ecs check --ansi", + "fix-cs": "vendor/bin/ecs check --fix --ansi", "phpstan": "vendor/bin/phpstan --ansi" } } From d4266e3c851660ef95563558e3ecc4588dbcaaf8 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 7 Feb 2022 11:12:33 +0100 Subject: [PATCH 07/10] [cs] enable docblock set --- .github/workflows/code_analysis.yaml | 2 +- ecs.php | 2 +- src/JSONDB.php | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index 763f18f..517f142 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -14,7 +14,7 @@ jobs: actions: - name: 'Coding Standard' - run: vendor/bin/ecs check + run: composer check-cs name: ${{ matrix.actions.name }} runs-on: ubuntu-latest diff --git a/ecs.php b/ecs.php index caba127..a8cd72d 100644 --- a/ecs.php +++ b/ecs.php @@ -23,6 +23,6 @@ // run and fix, one by one $containerConfigurator->import(SetList::SPACES); $containerConfigurator->import(SetList::ARRAY); - // $containerConfigurator->import(SetList::DOCBLOCK); + $containerConfigurator->import(SetList::DOCBLOCK); $containerConfigurator->import(SetList::PSR_12); }; diff --git a/src/JSONDB.php b/src/JSONDB.php index 3e68402..7bcd3be 100644 --- a/src/JSONDB.php +++ b/src/JSONDB.php @@ -66,7 +66,7 @@ private function check_file() * Checks and validates if JSON file exists * * @return bool - */ + */ // Checks if JSON file exists, if not create if (! file_exists($this->file)) { @@ -122,7 +122,7 @@ public function select($args = '*') * * @param type $args Optional. Default * * @return type object - */ + */ // Explode to array $this->select = explode(',', $args); @@ -141,7 +141,7 @@ public function from($file, $load = 'full') * * @param type $file. Accepts file path to jSON file * @return type object - */ + */ $this->file = sprintf('%s/%s.json', $this->dir, str_replace('.json', '', $file)); // Adding .json extension is no longer necessary @@ -167,7 +167,7 @@ public function where(array $columns, $merge = 'OR') } /** - * Implements regex search on where statement + * Implements regex search on where statement. * * @param string $pattern Regex pattern * @param int $preg_match_flags Flags for preg_grep(). See - https://www.php.net/manual/en/function.preg-match.php From 02bbff73682cc522e7cc9633ea582a37fef25356 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 7 Feb 2022 11:22:22 +0100 Subject: [PATCH 08/10] enable Common and Control structures set --- ecs.php | 9 ++++ src/JSONDB.php | 65 +++++++++++------------ tests/JSONDBTest.php | 122 +++++++++++++++++++++---------------------- 3 files changed, 103 insertions(+), 93 deletions(-) diff --git a/ecs.php b/ecs.php index a8cd72d..f6f8318 100644 --- a/ecs.php +++ b/ecs.php @@ -14,6 +14,12 @@ __DIR__ . '/tests', ]); + $parameters->set(Option::PARALLEL, true); + + $parameters->set(Option::SKIP, [ + \PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer::class, + ]); + $services = $containerConfigurator->services(); $services->set(ArraySyntaxFixer::class) ->call('configure', [[ @@ -24,5 +30,8 @@ $containerConfigurator->import(SetList::SPACES); $containerConfigurator->import(SetList::ARRAY); $containerConfigurator->import(SetList::DOCBLOCK); + $containerConfigurator->import(SetList::COMMENTS); + $containerConfigurator->import(SetList::CONTROL_STRUCTURES); + $containerConfigurator->import(SetList::PSR_12); }; diff --git a/src/JSONDB.php b/src/JSONDB.php index 7bcd3be..9f0d70a 100644 --- a/src/JSONDB.php +++ b/src/JSONDB.php @@ -35,9 +35,9 @@ class JSONDB public const DESC = 0; - public const AND = "AND"; + public const AND = 'AND'; - public const OR = "OR"; + public const OR = 'OR'; public function __construct($dir, $json_encode_opt = JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) { @@ -74,13 +74,14 @@ private function check_file() // $this->commit(); } - if ('partial' == $this->load) { + if ($this->load == 'partial') { $this->fp = fopen($this->file, 'r+'); if (! $this->fp) { throw new \Exception('Unable to open json file'); } - if (($size = $this->check_fp_size())) { + $size = $this->check_fp_size(); + if ($size) { $content = get_json_chunk($this->fp); // We could not get the first chunk of JSON. Lets try to load everything then @@ -113,6 +114,8 @@ private function check_file() $this->content = $content; return true; } + $this->content = $content; + return true; } public function select($args = '*') @@ -245,10 +248,10 @@ public function commit() $f = fopen($this->file, 'w+'); } - if ('full' === $this->load) { + if ($this->load === 'full') { // Write everything back into the file fwrite($f, (! $this->content ? '[]' : json_encode($this->content, $this->json_opts['encode']))); - } elseif ('partial' === $this->load) { + } elseif ($this->load === 'partial') { // Append it $this->append(); } else { @@ -275,16 +278,16 @@ private function append() while (($read = fread($this->fp, $per_read))) { $per_read = $i - $per_read < 0 ? $i : $per_read; - if (false === $lstblkbrkt) { + if ($lstblkbrkt === false) { $lstblkbrkt = strrpos($read, ']', 0); - if (false !== $lstblkbrkt) { + if ($lstblkbrkt !== false) { $lstblkbrkt = ($i - $per_read) + $lstblkbrkt; } } - if (false !== $lstblkbrkt) { + if ($lstblkbrkt !== false) { $lastinput = strrpos($read, '}'); - if (false !== $lastinput) { + if ($lastinput !== false) { $lastinput = ($i - $per_read) + $lastinput; break; } @@ -299,10 +302,10 @@ private function append() } } - if (false !== $lstblkbrkt) { + if ($lstblkbrkt !== false) { // We found existing json data, don't write extra [ $data = substr($data, 1); - if (false !== $lastinput) { + if ($lastinput !== false) { $data = sprintf(',%s', $data); } } else { @@ -423,25 +426,24 @@ private function where_result() { $this->flush_indexes(); - if ($this->merge == "AND") { + if ($this->merge == 'AND') { return $this->where_and_result(); - } else { - // Filter array - $r = array_filter($this->content, function ($row, $index) { - $row = (array) $row; // Convert first stage to array if object - - // Check for rows intersecting with the where values. - if (array_uintersect_uassoc($row, $this->where, [$this, "intersect_value_check"], "strcasecmp") /*array_intersect_assoc( $row, $this->where )*/) { - $this->last_indexes[] = $index; - return true; - } + } + // Filter array + $r = array_filter($this->content, function ($row, $index) { + $row = (array) $row; // Convert first stage to array if object - return false; - }, ARRAY_FILTER_USE_BOTH); + // Check for rows intersecting with the where values. + if (array_uintersect_uassoc($row, $this->where, [$this, 'intersect_value_check'], 'strcasecmp') /*array_intersect_assoc( $row, $this->where )*/) { + $this->last_indexes[] = $index; + return true; + } - // Make sure every object is turned to array here. - return array_values(obj_to_array($r)); - } + return false; + }, ARRAY_FILTER_USE_BOTH); + + // Make sure every object is turned to array here. + return array_values(obj_to_array($r)); } /** @@ -463,7 +465,7 @@ private function where_and_result() $row = (array) $row; //check if the row = where['col'=>'val', 'col2'=>'val2'] - if (! array_udiff_uassoc($this->where, $row, [$this, "intersect_value_check"], "strcasecmp")) { + if (! array_udiff_uassoc($this->where, $row, [$this, 'intersect_value_check'], 'strcasecmp')) { $r[] = $row; // Append also each row array key $this->last_indexes[] = $index; @@ -538,15 +540,14 @@ public function to_mysql(string $from, string $to, bool $create_table = true): b }, array_values($row)); $cols = array_map(function ($col) { - return sprintf("`%s`", $col); + return sprintf('`%s`', $col); }, array_keys($row)); $sql .= sprintf("INSERT INTO `%s` ( %s ) VALUES ( %s );\n", $table, implode(', ', $cols), implode(', ', $values)); } file_put_contents($to, $sql); return true; - } else { - return false; } + return false; } private function _to_mysql_type($type) diff --git a/tests/JSONDBTest.php b/tests/JSONDBTest.php index 9046d90..fa8de5e 100644 --- a/tests/JSONDBTest.php +++ b/tests/JSONDBTest.php @@ -46,10 +46,10 @@ public function testInsert(): void ], 'AND') ->get(); - $this->db->insert("users", [ - "name" => "Dummy", - "state" => "Lagos", - "age" => 12, + $this->db->insert('users', [ + 'name' => 'Dummy', + 'state' => 'Lagos', + 'age' => 12, ]); $this->assertEquals($name, $user[0]['name']); @@ -98,101 +98,101 @@ public function testWhere(): void public function testMultiWhere(): void { - $this->db->insert("users", [ - "name" => "Jajo", - "age" => null, - "state" => "Lagos", + $this->db->insert('users', [ + 'name' => 'Jajo', + 'age' => null, + 'state' => 'Lagos', ]); - $this->db->insert("users", [ - "name" => "Johnny", - "age" => 30, - "state" => "Ogun", + $this->db->insert('users', [ + 'name' => 'Johnny', + 'age' => 30, + 'state' => 'Ogun', ]); - $result = $this->db->select("*")->from("users")->where([ - "age" => null, - "name" => "Jajo", + $result = $this->db->select('*')->from('users')->where([ + 'age' => null, + 'name' => 'Jajo', ])->get(); $this->assertEquals('Jajo', $result[0]['name']); } public function testAND(): void { - $this->db->insert("users", [ - "name" => "Jajo", - "age" => 50, - "state" => "Lagos", + $this->db->insert('users', [ + 'name' => 'Jajo', + 'age' => 50, + 'state' => 'Lagos', ]); - $this->db->insert("users", [ - "name" => "Johnny", - "age" => 50, - "state" => "Ogun", + $this->db->insert('users', [ + 'name' => 'Johnny', + 'age' => 50, + 'state' => 'Ogun', ]); - $result = $this->db->select("*")->from("users")->where([ - "age" => 50, - "name" => "Jajo", + $result = $this->db->select('*')->from('users')->where([ + 'age' => 50, + 'name' => 'Jajo', ], JSONDB::AND)->get(); $this->assertEquals(1, count($result)); - $this->assertEquals("Jajo", $result[0]['name']); + $this->assertEquals('Jajo', $result[0]['name']); } public function testRegexAND(): void { - $this->db->insert("users", [ - "name" => "Paulo", - "age" => 50, - "state" => "Algeria", + $this->db->insert('users', [ + 'name' => 'Paulo', + 'age' => 50, + 'state' => 'Algeria', ]); - $this->db->insert("users", [ - "name" => "Nina", - "age" => 50, - "state" => "Nigeria", + $this->db->insert('users', [ + 'name' => 'Nina', + 'age' => 50, + 'state' => 'Nigeria', ]); - $this->db->insert("users", [ - "name" => "Ogwo", - "age" => 49, - "state" => "Nigeria", + $this->db->insert('users', [ + 'name' => 'Ogwo', + 'age' => 49, + 'state' => 'Nigeria', ]); $result = ( - $this->db->select("*") - ->from("users") + $this->db->select('*') + ->from('users') ->where([ - "state" => JSONDB::regex("/ria/"), - "age" => JSONDB::regex("/5[0-9]/"), + 'state' => JSONDB::regex('/ria/'), + 'age' => JSONDB::regex('/5[0-9]/'), ], JSONDB::AND) ->get() ); $this->assertEquals(2, count($result)); - $this->assertEquals("Paulo", $result[0]["name"]); - $this->assertEquals("Nina", $result[1]["name"]); + $this->assertEquals('Paulo', $result[0]['name']); + $this->assertEquals('Nina', $result[1]['name']); } public function testRegex(): void { - $this->db->insert("users", [ - "name" => "Jajo", - "age" => 89, - "state" => "Abia", + $this->db->insert('users', [ + 'name' => 'Jajo', + 'age' => 89, + 'state' => 'Abia', ]); - $this->db->insert("users", [ - "name" => "Mitchell", - "age" => 45, - "state" => "Zamfara", + $this->db->insert('users', [ + 'name' => 'Mitchell', + 'age' => 45, + 'state' => 'Zamfara', ]); - $result = ($this->db->select("*") - ->from("users") + $result = ($this->db->select('*') + ->from('users') ->where([ - "state" => JSONDB::regex("/Zam/"), + 'state' => JSONDB::regex('/Zam/'), ]) ->get()); @@ -212,11 +212,11 @@ public function testUpdate(): void ->trigger(); $this->db->update([ - "state" => "Rivers", + 'state' => 'Rivers', ]) - ->from("users") + ->from('users') ->where([ - "name" => "Dummy", + 'name' => 'Dummy', ]) ->trigger(); @@ -232,9 +232,9 @@ public function testUpdate(): void public function testSQLExport(): void { - $this->db->to_mysql("users", "tests/users.sql"); + $this->db->to_mysql('users', 'tests/users.sql'); - $this->assertTrue(file_exists("tests/users.sql")); + $this->assertTrue(file_exists('tests/users.sql')); } public function testDelete(): void From e22e81eda600b0052bb13b3cfdbecbc4452280de Mon Sep 17 00:00:00 2001 From: James John Date: Mon, 28 Feb 2022 08:03:43 +0000 Subject: [PATCH 09/10] Maintain insert() method returning void --- src/JSONDB.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/JSONDB.php b/src/JSONDB.php index 9f0d70a..22d657b 100644 --- a/src/JSONDB.php +++ b/src/JSONDB.php @@ -203,7 +203,6 @@ public function update(array $columns) * @param string $file json filename without extension * @param array $values Array of columns as keys and values * - * @return array $last_indexes Array of last index inserted */ public function insert($file, array $values) { @@ -234,10 +233,7 @@ public function insert($file, array $values) } $this->content[] = $values; - // $this->last_indexes = [ ( count( $this->content ) - 1 ) ]; $this->commit(); - - return $this->last_indexes; } public function commit() From 41cf84e43811ef18dcb5981be59c3098e86e03c1 Mon Sep 17 00:00:00 2001 From: James John Date: Mon, 28 Feb 2022 08:06:14 +0000 Subject: [PATCH 10/10] Apply phpstan suggestion --- src/JSONDB.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/JSONDB.php b/src/JSONDB.php index 22d657b..ff77282 100644 --- a/src/JSONDB.php +++ b/src/JSONDB.php @@ -114,8 +114,6 @@ private function check_file() $this->content = $content; return true; } - $this->content = $content; - return true; } public function select($args = '*')