From e239d23d8b791ef65fffd4781c9a486d4fcef245 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 1 Mar 2024 00:45:22 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Phenomine/Contracts/Command/Command.php | 1 - src/Phenomine/Support/Application.php | 11 ++- src/Phenomine/Support/Collection.php | 60 +++++++++---- .../Controller/MakeControllerCommand.php | 1 + .../Migration/MakeMigrationCommand.php | 5 +- .../Migration/RollbackMigrationCommand.php | 1 + .../Migration/RunMigrationCommand.php | 13 +-- .../Commands/Model/MakeModelCommand.php | 17 ++-- .../Commands/Seeder/MakeSeederCommand.php | 3 +- .../Commands/Seeder/RunSeederCommand.php | 4 +- .../Support/Database/SeederRunner.php | 1 + src/Phenomine/Support/Env.php | 9 +- .../Support/Exceptions/ExceptionHandler.php | 23 ++--- src/Phenomine/Support/Model.php | 90 ++++++++++++------- src/Phenomine/Support/Request.php | 1 - src/Phenomine/Support/Seeder.php | 13 +-- 16 files changed, 155 insertions(+), 98 deletions(-) diff --git a/src/Phenomine/Contracts/Command/Command.php b/src/Phenomine/Contracts/Command/Command.php index de6dbf1..42959e3 100644 --- a/src/Phenomine/Contracts/Command/Command.php +++ b/src/Phenomine/Contracts/Command/Command.php @@ -19,7 +19,6 @@ class Command extends SymfonyCommand public function __construct() { - // call warn production mode if function exists if (method_exists($this, 'warnProductionMode')) { $this->warnProductionMode(); diff --git a/src/Phenomine/Support/Application.php b/src/Phenomine/Support/Application.php index c980b22..7fcdb80 100644 --- a/src/Phenomine/Support/Application.php +++ b/src/Phenomine/Support/Application.php @@ -2,7 +2,6 @@ namespace Phenomine\Support; -use Exception; use Phenomine\Contracts\Application\ApplicationContract; use Phenomine\Support\Exceptions\ExceptionHandler; @@ -15,7 +14,6 @@ public function run() $_app = $this; $this->console->run(); } else { - ob_start(); // start output buffering global $_app; @@ -25,7 +23,8 @@ public function run() } } - public function prepare() { + public function prepare() + { $handler = new ExceptionHandler(); set_error_handler([$handler, 'errorHandler']); @@ -36,12 +35,12 @@ public function init() { $this->prepare(); new Env(); // load .env file - $env = env('APP_ENV', ""); - if ($env == "") { + $env = env('APP_ENV', ''); + if ($env == '') { throw new \Exception('Application environment not set correctly. Please check your configuration before running application.'); } - if ($env == "production") { + if ($env == 'production') { error_reporting(0); ini_set('display_errors', 0); ini_set('log_errors', 1); diff --git a/src/Phenomine/Support/Collection.php b/src/Phenomine/Support/Collection.php index 405156a..1d36a27 100644 --- a/src/Phenomine/Support/Collection.php +++ b/src/Phenomine/Support/Collection.php @@ -3,15 +3,19 @@ namespace Phenomine\Support; #[\AllowDynamicProperties] -class Collection { +class Collection +{ protected $items; - public function __construct($items = []) { + public function __construct($items = []) + { $this->items = $items; + return $this; } - public function __get($name) { + public function __get($name) + { // remove scalar type if (is_array($this->items)) { return (object) $this->items; @@ -20,7 +24,8 @@ public function __get($name) { } } - public function __set($name, $value) { + public function __set($name, $value) + { if (!$this->items) { $this->items = []; } @@ -28,11 +33,13 @@ public function __set($name, $value) { $this->items[$name] = $value; } - public function count() { + public function count() + { return count($this->items); } - public function first() { + public function first() + { if (empty($this->items)) { return null; } @@ -44,10 +51,12 @@ public function first() { return $first; } } + return null; } - public function last() { + public function last() + { if (empty($this->items)) { return null; } @@ -59,56 +68,69 @@ public function last() { return $last; } } + return null; } - public function pop() { + public function pop() + { return array_pop($this->items); } - public function shift() { + public function shift() + { return array_shift($this->items); } - public function unshift($item) { + public function unshift($item) + { array_unshift($this->items, $item); } - public function has($key) { + public function has($key) + { return isset($this->items[$key]); } - public function remove($key) { + public function remove($key) + { unset($this->items[$key]); } - public function keys() { + public function keys() + { return array_keys($this->items); } - public function values() { + public function values() + { return array_values($this->items); } - public function map($callback) { + public function map($callback) + { return new static(array_map($callback, $this->items)); } - public function filter($callback) { + public function filter($callback) + { return new static(array_filter($this->items, $callback)); } - public function each($callback) { + public function each($callback) + { foreach ($this->items as $key => $item) { $callback($item, $key); } } - public function merge($items) { + public function merge($items) + { return new static(array_merge($this->items, $items)); } - public function toArray() { + public function toArray() + { return $this->items; } } diff --git a/src/Phenomine/Support/Console/Commands/Controller/MakeControllerCommand.php b/src/Phenomine/Support/Console/Commands/Controller/MakeControllerCommand.php index 710b9de..26fb4c0 100644 --- a/src/Phenomine/Support/Console/Commands/Controller/MakeControllerCommand.php +++ b/src/Phenomine/Support/Console/Commands/Controller/MakeControllerCommand.php @@ -22,6 +22,7 @@ public function handle() $result = $this->confirm('Do you want to continue?'); if (!$result) { $this->warn('Operation cancelled.'); + return; } } diff --git a/src/Phenomine/Support/Console/Commands/Migration/MakeMigrationCommand.php b/src/Phenomine/Support/Console/Commands/Migration/MakeMigrationCommand.php index 04cf086..ed8c198 100644 --- a/src/Phenomine/Support/Console/Commands/Migration/MakeMigrationCommand.php +++ b/src/Phenomine/Support/Console/Commands/Migration/MakeMigrationCommand.php @@ -17,7 +17,6 @@ class MakeMigrationCommand extends Command 'm' => 'Make a model for the table', ]; - public function handle() { if (config('app.env') == 'production') { @@ -26,6 +25,7 @@ public function handle() $result = $this->confirm('Do you want to continue?'); if (!$result) { $this->warn('Operation cancelled.'); + return; } } @@ -50,12 +50,11 @@ public function handle() } $fileModel = File::createFileFromString(base_path('app/Models'), $tableName, '.php'); $stubModel = File::readAndReplace(__DIR__.'../../../../../Stubs/model.stub', [ - 'class' => $tableName + 'class' => $tableName, ]); File::write($fileModel['file'], $stubModel); } - $this->info('Migration created successfully'); return true; diff --git a/src/Phenomine/Support/Console/Commands/Migration/RollbackMigrationCommand.php b/src/Phenomine/Support/Console/Commands/Migration/RollbackMigrationCommand.php index 80a0ccc..657277e 100644 --- a/src/Phenomine/Support/Console/Commands/Migration/RollbackMigrationCommand.php +++ b/src/Phenomine/Support/Console/Commands/Migration/RollbackMigrationCommand.php @@ -20,6 +20,7 @@ public function handle() $result = $this->confirm('Do you want to continue?'); if (!$result) { $this->warn('Operation cancelled.'); + return; } } diff --git a/src/Phenomine/Support/Console/Commands/Migration/RunMigrationCommand.php b/src/Phenomine/Support/Console/Commands/Migration/RunMigrationCommand.php index 708b64d..23cf028 100644 --- a/src/Phenomine/Support/Console/Commands/Migration/RunMigrationCommand.php +++ b/src/Phenomine/Support/Console/Commands/Migration/RunMigrationCommand.php @@ -22,6 +22,7 @@ public function handle() $result = $this->confirm('Do you want to continue?'); if (!$result) { $this->warn('Operation cancelled.'); + return; } } @@ -34,20 +35,22 @@ public function handle() // only support for mysql, mariadb, and postgresql $db = config('database.driver'); if ($db != 'mysql' && $db != 'mariadb' && $db != 'pgsql') { - $this->error('Database "' . config('database.database') . '" is not exist. Please create it manually.'); + $this->error('Database "'.config('database.database').'" is not exist. Please create it manually.'); + return; } else { // ask for create database - $create = $this->confirm('Database "' . config('database.database') . '" is not exist. Would you like to create it?'); + $create = $this->confirm('Database "'.config('database.database').'" is not exist. Would you like to create it?'); if (!$create) { $this->warn('Migration cancelled.'); + return; } // create database - $query = 'CREATE DATABASE ' . config('database.database'); + $query = 'CREATE DATABASE '.config('database.database'); if ($db == 'mysql' || $db == 'mariadb') { - $query .= ' CHARACTER SET ' . config('database.charset', 'utf8mb4') . ' COLLATE ' . config('database.collation', 'utf8mb4_unicode_ci'); + $query .= ' CHARACTER SET '.config('database.charset', 'utf8mb4').' COLLATE '.config('database.collation', 'utf8mb4_unicode_ci'); } // manually run query @@ -61,7 +64,7 @@ public function handle() $pdo->exec($query); $pdo = null; - $this->info('Database "' . config('database.database') . '" created.'); + $this->info('Database "'.config('database.database').'" created.'); } } } diff --git a/src/Phenomine/Support/Console/Commands/Model/MakeModelCommand.php b/src/Phenomine/Support/Console/Commands/Model/MakeModelCommand.php index cff9895..3e46ca7 100644 --- a/src/Phenomine/Support/Console/Commands/Model/MakeModelCommand.php +++ b/src/Phenomine/Support/Console/Commands/Model/MakeModelCommand.php @@ -17,7 +17,6 @@ class MakeModelCommand extends Command 'm' => 'Make a migration for the table', ]; - public function handle() { if (config('app.env') == 'production') { @@ -26,6 +25,7 @@ public function handle() $result = $this->confirm('Do you want to continue?'); if (!$result) { $this->warn('Operation cancelled.'); + return; } } @@ -38,24 +38,23 @@ public function handle() } $fileModel = File::createFileFromString(base_path('app/Models'), $tableName, '.php'); $stubModel = File::readAndReplace(__DIR__.'../../../../../Stubs/model.stub', [ - 'class' => $tableName + 'class' => $tableName, ]); File::write($fileModel['file'], $stubModel); // check if option migration is set if ($this->option('m')) { $name = 'migration_'.date('YmdHis').'_'.$this->argument('table'); - $file = File::createFileFromString(base_path('db/migrations'), $name, '.php'); + $file = File::createFileFromString(base_path('db/migrations'), $name, '.php'); - $stub = File::readAndReplace(__DIR__.'../../../../../Stubs/migration.stub', [ - 'migration' => $name, - 'table' => $this->argument('table'), - ]); + $stub = File::readAndReplace(__DIR__.'../../../../../Stubs/migration.stub', [ + 'migration' => $name, + 'table' => $this->argument('table'), + ]); - File::write($file['file'], $stub); + File::write($file['file'], $stub); } - $this->info('Model created successfully'); return true; diff --git a/src/Phenomine/Support/Console/Commands/Seeder/MakeSeederCommand.php b/src/Phenomine/Support/Console/Commands/Seeder/MakeSeederCommand.php index a8a3dd8..1fd170d 100644 --- a/src/Phenomine/Support/Console/Commands/Seeder/MakeSeederCommand.php +++ b/src/Phenomine/Support/Console/Commands/Seeder/MakeSeederCommand.php @@ -22,6 +22,7 @@ public function handle() $result = $this->confirm('Do you want to continue?'); if (!$result) { $this->warn('Operation cancelled.'); + return; } } @@ -30,7 +31,7 @@ public function handle() $file = File::createFileFromString(base_path('db/seeders'), $name, '.php'); $stub = File::readAndReplace(__DIR__.'../../../../../Stubs/seeder.stub', [ - 'class' => $name + 'class' => $name, ]); File::write($file['file'], $stub); diff --git a/src/Phenomine/Support/Console/Commands/Seeder/RunSeederCommand.php b/src/Phenomine/Support/Console/Commands/Seeder/RunSeederCommand.php index f9223ab..627ebf6 100644 --- a/src/Phenomine/Support/Console/Commands/Seeder/RunSeederCommand.php +++ b/src/Phenomine/Support/Console/Commands/Seeder/RunSeederCommand.php @@ -3,7 +3,6 @@ namespace Phenomine\Support\Console\Commands\Seeder; use Phenomine\Contracts\Command\Command; -use Phenomine\Support\File; use Phenomine\Support\Database\SeederRunner; class RunSeederCommand extends Command @@ -21,16 +20,17 @@ public function handle() $result = $this->confirm('Do you want to continue?'); if (!$result) { $this->warn('Operation cancelled.'); + return; } } - $this->runner = new SeederRunner(); $seeders = $this->runner->getSeederFiles(); if (count($seeders) == 0) { $this->info('Nothing to seed'); + return true; } $counter = 0; diff --git a/src/Phenomine/Support/Database/SeederRunner.php b/src/Phenomine/Support/Database/SeederRunner.php index b96878d..4e38a16 100644 --- a/src/Phenomine/Support/Database/SeederRunner.php +++ b/src/Phenomine/Support/Database/SeederRunner.php @@ -16,6 +16,7 @@ public function run($seeder) $runner = new $seeder['class'](); $runner->run(); + return [ 'success' => true, 'status' => 'success', diff --git a/src/Phenomine/Support/Env.php b/src/Phenomine/Support/Env.php index 7b2919d..4bf821d 100644 --- a/src/Phenomine/Support/Env.php +++ b/src/Phenomine/Support/Env.php @@ -9,14 +9,15 @@ namespace Phenomine\Support; -class Env { - +class Env +{ public function __construct() { $this->load(); } - public function load() { + public function load() + { global $_ENV; $env = file_get_contents(base_path('.env')); $env = explode("\n", $env); @@ -43,6 +44,7 @@ public function load() { public static function get($key, $default = null) { global $_ENV; + return $_ENV[$key] ?? $default; } @@ -55,6 +57,7 @@ public static function set($key, $value) public static function has($key) { global $_ENV; + return isset($_ENV[$key]); } } diff --git a/src/Phenomine/Support/Exceptions/ExceptionHandler.php b/src/Phenomine/Support/Exceptions/ExceptionHandler.php index a3bd89b..7473549 100644 --- a/src/Phenomine/Support/Exceptions/ExceptionHandler.php +++ b/src/Phenomine/Support/Exceptions/ExceptionHandler.php @@ -4,14 +4,15 @@ use Phenomine\Support\View; -class ExceptionHandler { - - function errorHandler(int $errNo, string $errMsg, string $file, int $line) { +class ExceptionHandler +{ + public function errorHandler(int $errNo, string $errMsg, string $file, int $line) + { echo "$errMsg in $file on line $line"; } - public function exceptionHandler($exception) { - + public function exceptionHandler($exception) + { if (env('APP_ENV') == 'production') { abort(500); exit; @@ -24,12 +25,12 @@ public function exceptionHandler($exception) { // remove the root path from the file path $file = str_replace($root, '', $file); $data = [ - 'type' => get_class($exception), - 'message' => $exception->getMessage(), - 'file' => $file, - 'line' => $exception->getLine(), - 'trace' => $exception->getTrace(), - 'traceAsString' => $exception->getTraceAsString() + 'type' => get_class($exception), + 'message' => $exception->getMessage(), + 'file' => $file, + 'line' => $exception->getLine(), + 'trace' => $exception->getTrace(), + 'traceAsString' => $exception->getTraceAsString(), ]; ob_end_clean(); $view = new View(__DIR__.'/../../views'); diff --git a/src/Phenomine/Support/Model.php b/src/Phenomine/Support/Model.php index 35585e2..d11d55b 100644 --- a/src/Phenomine/Support/Model.php +++ b/src/Phenomine/Support/Model.php @@ -3,8 +3,8 @@ namespace Phenomine\Support; #[\AllowDynamicProperties] -class Model { - +class Model +{ protected $table; protected $primaryKey = 'id'; protected $primaryType = 'int'; @@ -15,17 +15,20 @@ class Model { protected $data; - public function __construct() { + public function __construct() + { $name = strtolower((new \ReflectionClass($this))->getShortName()); $name = ngettext($name, $name.'s', 2); $this->table = $this->table ?? $name; } - public function __get($name) { + public function __get($name) + { return $this->data->$name ?? null; } - public function __set($name, $value) { + public function __set($name, $value) + { if (!$this->data) { $this->data = new Collection(); } @@ -35,7 +38,7 @@ public function __set($name, $value) { } else { $new_arr = []; $arr = $this->data->toArray(); - foreach($arr as $data) { + foreach ($arr as $data) { if (is_array($data)) { $data[$name] = $value; $new_arr[] = $data; @@ -49,26 +52,31 @@ public function __set($name, $value) { } } - public static function all() { - $model = new static; + public static function all() + { + $model = new static(); $model->data = db()->select($model->table, '*'); $model->data = new Collection($model->data); + return $model->data; } - public static function find($id) { - $model = new static; + public static function find($id) + { + $model = new static(); $model->data = db()->select($model->table, '*', [$model->primaryKey => $id]); $model->data = new Collection($model->data); $model->data = $model->data->first(); + return $model; } - public static function create($data) { - $model = new static; + public static function create($data) + { + $model = new static(); // only fillable allowed - $data = array_filter($data, function($key) use ($model) { + $data = array_filter($data, function ($key) use ($model) { return in_array($key, $model->fillable); }, ARRAY_FILTER_USE_KEY); @@ -80,53 +88,65 @@ public static function create($data) { } } - public function first() { + public function first() + { $this->first = true; $this->data = $this->data->first(); + return $this; } - public static function where($column, $value) { - $model = new static; + public static function where($column, $value) + { + $model = new static(); $model->data = db()->select($model->table, '*', [$column => $value]); $model->data = new Collection($model->data); + return $model; } - public function get() { + public function get() + { return $this->data; } - public function update($data) { + public function update($data) + { $arr_data = $this->data->toArray(); - foreach($arr_data as $data_want_to_update) { + foreach ($arr_data as $data_want_to_update) { if (is_array($data_want_to_update)) { db()->update($this->table, $data, [$this->primaryKey => $data_want_to_update[$this->primaryKey]]); } else { db()->delete($this->table, $data, [$this->primaryKey => $data_want_to_update]); } } + return true; } - public function delete() { + public function delete() + { $arr_data = $this->data->toArray(); - foreach($arr_data as $data) { + foreach ($arr_data as $data) { if (is_array($data)) { db()->delete($this->table, [$this->primaryKey => $data[$this->primaryKey]]); } else { db()->delete($this->table, [$this->primaryKey => $data]); } } + return true; } - public static function destroy($id) { - $model = new static; + public static function destroy($id) + { + $model = new static(); + return db()->delete($model->table, [$model->primaryKey => $id]); } - public function save() { + public function save() + { $data = $this->data->toArray(); if ($this->first) { if (isset($data[$this->primaryKey])) { @@ -136,7 +156,7 @@ public function save() { db()->insert($this->table, $data, $this->primaryKey); } } else { - foreach($data as $d) { + foreach ($data as $d) { if (isset($d[$this->primaryKey])) { $id = $d[$this->primaryKey]; db()->update($this->table, $d, [$this->primaryKey => $id]); @@ -147,27 +167,33 @@ public function save() { } } - public function __call($method, $parameters) { + public function __call($method, $parameters) + { return db()->$method($this->table, ...$parameters); } - public static function __callStatic($method, $parameters) { - return db()->$method((new static)->table, ...$parameters); + public static function __callStatic($method, $parameters) + { + return db()->$method((new static())->table, ...$parameters); } - public function __toString() { + public function __toString() + { return json_encode($this->data); } - public function toArray() { + public function toArray() + { return (array) $this->data; } - public function toJson() { + public function toJson() + { return json_encode($this->data); } - public function __invoke() { + public function __invoke() + { return $this->data; } } diff --git a/src/Phenomine/Support/Request.php b/src/Phenomine/Support/Request.php index 6b48284..b1de5fd 100644 --- a/src/Phenomine/Support/Request.php +++ b/src/Phenomine/Support/Request.php @@ -266,7 +266,6 @@ public static function abort($code) // abort and remove all response buffer ob_end_clean(); - $view = new View(__DIR__.'/../views'); $view->render('errors.'.$code); exit; diff --git a/src/Phenomine/Support/Seeder.php b/src/Phenomine/Support/Seeder.php index 0cb54d9..55fccd7 100644 --- a/src/Phenomine/Support/Seeder.php +++ b/src/Phenomine/Support/Seeder.php @@ -2,18 +2,21 @@ namespace Phenomine\Support; -class Seeder { - public function run() { +class Seeder +{ + public function run() + { // } - public function call($seeder) { + public function call($seeder) + { if (is_array($seeder)) { - foreach($seeder as $seed) { + foreach ($seeder as $seed) { $this->call($seed); } } else { - $seeder = new $seeder; + $seeder = new $seeder(); $seeder->run(); } }