From b19da2be7e09e4da5ddfc5f7880a72e454c6336c Mon Sep 17 00:00:00 2001 From: Robert M <48888686+robertmarney@users.noreply.github.com> Date: Sat, 18 Feb 2023 06:44:42 -0700 Subject: [PATCH] Upgrade to support Laravel 10 * L10 Upgrade * Add return types and hinting. * Modernize php language norms. * No change to output / functionality. --- .github/workflows/run-tests.yml | 5 +- .gitignore | 1 + build/.gitignore | 1 + composer.json | 19 +- phpstan-baseline.neon | 0 phpstan.neon.dist | 12 ++ rector.php | 31 +++ src/Orangehill/Iseed/Exceptions.php | 3 +- src/Orangehill/Iseed/Facades/Iseed.php | 3 +- src/Orangehill/Iseed/Iseed.php | 181 +++++++----------- src/Orangehill/Iseed/IseedCommand.php | 107 +++++------ src/Orangehill/Iseed/IseedServiceProvider.php | 144 +++++++------- src/config/config.php | 7 +- tests/IseedTest.php | 7 +- 14 files changed, 255 insertions(+), 266 deletions(-) create mode 100644 build/.gitignore create mode 100644 phpstan-baseline.neon create mode 100644 phpstan.neon.dist create mode 100644 rector.php diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 7d5a9d2..c77e1bc 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,7 +9,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - php: [8.2, 8.1, 8.0] + php: [8.2, 8.1] laravel: [10.*, 9.*] dependency-version: [prefer-lowest, prefer-stable] include: @@ -19,7 +19,8 @@ jobs: testbench: 7.* exclude: - laravel: 10.* - php: 8.0 + php: 8.0.2 + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} diff --git a/.gitignore b/.gitignore index 456bc4d..e2b6592 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ composer.lock .DS_Store .idea .phpunit.* +/build diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/build/.gitignore @@ -0,0 +1 @@ +* diff --git a/composer.json b/composer.json index c528c91..ebae6fb 100644 --- a/composer.json +++ b/composer.json @@ -10,21 +10,22 @@ } ], "require": { - "php": "^7.2|^8.0.2|^8.1", - "illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0" + "php": "^8.0.2|^8.1|^8.2", + "illuminate/support": "^8.0|^9.0|^10.0" }, "require-dev": { - "mockery/mockery": "^1.0.0", - "illuminate/filesystem": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0", - "laravel/framework": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/filesystem": "^8.0|^9.0|^10.0", + "laravel/framework": "^8.0|^9.0|^10.0", "laravel/pint": "^1.0", + "mockery/mockery": "^1.0.0", "nunomaduro/collision": "^6.0", "nunomaduro/larastan": "^2.0.1", "orchestra/testbench": "^7.0|^8.0", "phpstan/extension-installer": "^1.1", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "rector/rector": "^0.15.17" }, "autoload": { "psr-0": { @@ -39,6 +40,12 @@ "Orangehill\\Iseed\\Tests\\": "tests" } }, + "scripts": { + "analyse": "vendor/bin/phpstan analyse", + "test": "vendor/bin/phpunit", + "test-coverage": "vendor/bin/phpunit --coverage", + "format": "vendor/bin/pint" + }, "extra": { "laravel": { "providers": [ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..e69de29 diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..9914a5e --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,12 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 4 + paths: + - src + tmpDir: build/phpstan + checkOctaneCompatibility: true + checkModelProperties: true + checkMissingIterableValueType: false + diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..3f7237b --- /dev/null +++ b/rector.php @@ -0,0 +1,31 @@ +paths([ + __DIR__.'/src', + __DIR__.'/tests', + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + + $rectorConfig->rules([ + ReturnTypeFromStrictNativeCallRector::class, + ReturnTypeFromStrictScalarReturnExprRector::class, + Rector\DeadCode\Rector\Cast\RecastingRemovalRector::class, + Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector::class, + ]); + +// define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_81, + ]); +}; diff --git a/src/Orangehill/Iseed/Exceptions.php b/src/Orangehill/Iseed/Exceptions.php index cd10130..8b95909 100644 --- a/src/Orangehill/Iseed/Exceptions.php +++ b/src/Orangehill/Iseed/Exceptions.php @@ -4,5 +4,4 @@ class TableNotFoundException extends \RuntimeException { - -} \ No newline at end of file +} diff --git a/src/Orangehill/Iseed/Facades/Iseed.php b/src/Orangehill/Iseed/Facades/Iseed.php index a950e55..4c60f01 100644 --- a/src/Orangehill/Iseed/Facades/Iseed.php +++ b/src/Orangehill/Iseed/Facades/Iseed.php @@ -6,7 +6,6 @@ class Iseed extends Facade { - /** * Get the registered name of the component. * @@ -16,4 +15,4 @@ protected static function getFacadeAccessor() { return 'iseed'; } -} \ No newline at end of file +} diff --git a/src/Orangehill/Iseed/Iseed.php b/src/Orangehill/Iseed/Iseed.php index 20e5c8e..41c55c0 100644 --- a/src/Orangehill/Iseed/Iseed.php +++ b/src/Orangehill/Iseed/Iseed.php @@ -2,40 +2,35 @@ namespace Orangehill\Iseed; +use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Composer; -use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Schema; class Iseed { /** * Name of the database upon which the seed will be executed. - * - * @var string */ - protected $databaseName; + protected string $databaseName; /** * New line character for seed files. * Double quotes are mandatory! - * - * @var string */ - private $newLineCharacter = PHP_EOL; + private string $newLineCharacter = PHP_EOL; /** * Desired indent for the code. * For tabulator use \t * Double quotes are mandatory! - * - * @var string */ - private $indentCharacter = " "; + private string $indentCharacter = ' '; - /** - * @var Composer - */ - private $composer; + private readonly Composer $composer; + + private Filesystem $files; public function __construct(Filesystem $filesystem = null, Composer $composer = null) { @@ -43,34 +38,31 @@ public function __construct(Filesystem $filesystem = null, Composer $composer = $this->composer = $composer ?: new Composer($this->files); } - public function readStubFile($file) + public function readStubFile($file): string { $buffer = file($file, FILE_IGNORE_NEW_LINES); + return implode(PHP_EOL, $buffer); } /** * Generates a seed file. - * @param string $table - * @param string $prefix - * @param string $suffix - * @param string $database - * @param int $max - * @param string $prerunEvent - * @param string $postunEvent - * @return bool - * @throws Orangehill\Iseed\TableNotFoundException + * + * @param int $max + * @param null $exclude + * @param null $prerunEvent + * @param null $postrunEvent */ - public function generateSeed($table, $prefix=null, $suffix=null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC') + public function generateSeed(string $table, string $prefix = null, string $suffix = null, string $database = null, int|string $max = 0, int $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, bool $dumpAuto = true, bool $indexed = true, ?string $orderBy = null, string $direction = 'ASC'): bool { - if (!$database) { + if (! $database) { $database = config('database.default'); } $this->databaseName = $database; // Check if table exists - if (!$this->hasTable($table)) { + if (! $this->hasTable($table)) { throw new TableNotFoundException("Table $table was not found."); } @@ -84,7 +76,7 @@ public function generateSeed($table, $prefix=null, $suffix=null, $database = nul $className = $this->generateClassName($table, $prefix, $suffix); // Get template for a seed file contents - $stub = $this->readStubFile($this->getStubPath() . '/seed.stub'); + $stub = $this->readStubFile($this->getStubPath().'/seed.stub'); // Get a seed folder path $seedPath = $this->getSeedPath(); @@ -118,28 +110,29 @@ public function generateSeed($table, $prefix=null, $suffix=null, $database = nul /** * Get a seed folder path + * * @return string */ public function getSeedPath() { - return base_path() . config('iseed::config.path'); + return base_path().config('iseed::config.path'); } /** * Get the Data - * @param string $table - * @return Array + * + * @param int $max */ - public function getData($table, $max, $exclude = null, $orderBy = null, $direction = 'ASC') + public function getData(string $table, int|string $max, ?array $exclude = null, ?string $orderBy = null, ?string $direction = 'ASC'): array { - $result = \DB::connection($this->databaseName)->table($table); + $result = DB::connection($this->databaseName)->table($table); - if (!empty($exclude)) { - $allColumns = \DB::connection($this->databaseName)->getSchemaBuilder()->getColumnListing($table); + if (! empty($exclude)) { + $allColumns = DB::connection($this->databaseName)->getSchemaBuilder()->getColumnListing($table); $result = $result->select(array_diff($allColumns, $exclude)); } - if($orderBy) { + if ($orderBy) { $result = $result->orderBy($orderBy, $direction); } @@ -147,80 +140,68 @@ public function getData($table, $max, $exclude = null, $orderBy = null, $directi $result = $result->limit($max); } - return $result->get(); + return $result->get()->toArray(); } /** * Repacks data read from the database - * @param array|object $data - * @return array */ - public function repackSeedData($data) + public function repackSeedData(object|array $data): array { - if (!is_array($data)) { + if (! is_array($data)) { $data = $data->toArray(); } - $dataArray = array(); - if (!empty($data)) { + $dataArray = []; + if (! empty($data)) { foreach ($data as $row) { - $rowArray = array(); + $rowArray = []; foreach ($row as $columnName => $columnValue) { $rowArray[$columnName] = $columnValue; } $dataArray[] = $rowArray; } } + return $dataArray; } /** * Checks if a database table exists - * @param string $table - * @return boolean */ - public function hasTable($table) + public function hasTable(string $table): bool { - return \Schema::connection($this->databaseName)->hasTable($table); + return Schema::connection($this->databaseName)->hasTable($table); } /** * Generates a seed class name (also used as a filename) - * @param string $table - * @param string $prefix - * @param string $suffix - * @return string */ - public function generateClassName($table, $prefix=null, $suffix=null) + public function generateClassName(string $table, string $prefix = null, string $suffix = null): string { $tableString = ''; $tableName = explode('_', $table); foreach ($tableName as $tableNameExploded) { $tableString .= ucfirst($tableNameExploded); } - return ($prefix ? $prefix : '') . ucfirst($tableString) . 'Table' . ($suffix ? $suffix : '') . 'Seeder'; + + return ($prefix ?: '').ucfirst($tableString).'Table'.($suffix ?: '').'Seeder'; } /** * Get the path to the stub file. - * @return string */ - public function getStubPath() + public function getStubPath(): string { - return __DIR__ . DIRECTORY_SEPARATOR . 'stubs'; + return __DIR__.DIRECTORY_SEPARATOR.'stubs'; } /** * Populate the place-holders in the seed stub. - * @param string $class - * @param string $stub - * @param string $table - * @param string $data - * @param int $chunkSize - * @param string $prerunEvent - * @param string $postunEvent - * @return string + * + * @param null|string $prerunEvent + * @param null|string $postrunEvent */ - public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null, $indexed = true) + public function populateStub(string $class, string $stub, string $table, array $data, int $chunkSize = null, string|null $prerunEvent = null, string|null $postrunEvent = null, bool $indexed = true): string { $chunkSize = $chunkSize ?: config('iseed::config.chunk_size'); @@ -256,9 +237,7 @@ public function populateStub($class, $stub, $table, $data, $chunkSize = null, $p '{{prerun_event}}', $prerunEventInsert, $stub ); - if (!is_null($table)) { - $stub = str_replace('{{table}}', $table, $stub); - } + $stub = str_replace('{{table}}', $table, $stub); $postrunEventInsert = ''; if ($postrunEvent) { @@ -278,28 +257,21 @@ public function populateStub($class, $stub, $table, $data, $chunkSize = null, $p '{{postrun_event}}', $postrunEventInsert, $stub ); - $stub = str_replace('{{insert_statements}}', $inserts, $stub); - - return $stub; + return str_replace('{{insert_statements}}', $inserts, $stub); } /** * Create the full path name to the seed file. - * @param string $name - * @param string $path - * @return string */ - public function getPath($name, $path) + public function getPath(string $name, string $path): string { - return $path . '/' . $name . '.php'; + return $path.'/'.$name.'.php'; } /** * Prettify a var_export of an array - * @param array $array - * @return string */ - protected function prettifyArray($array, $indexed = true) + protected function prettifyArray(array $array, bool $indexed = true): string { $content = ($indexed) ? var_export($array, true) @@ -313,7 +285,7 @@ protected function prettifyArray($array, $indexed = true) $lines[$i] = ltrim($lines[$i]); //Check for closing bracket - if (strpos($lines[$i], ')') !== false) { + if (str_contains($lines[$i], ')')) { $tabCount--; } @@ -328,31 +300,25 @@ protected function prettifyArray($array, $indexed = true) //skip character right after an escape \ if ($lines[$i][$j] == '\\') { $j++; - } - //check string open/end - else if ($lines[$i][$j] == '\'') { - $inString = !$inString; + } //check string open/end + elseif ($lines[$i][$j] == '\'') { + $inString = ! $inString; } } //check for openning bracket - if (strpos($lines[$i], '(') !== false) { + if (str_contains($lines[$i], '(')) { $tabCount++; } } - $content = implode("\n", $lines); - - return $content; + return implode("\n", $lines); } /** * Adds new lines to the passed content variable reference. - * - * @param string $content - * @param int $numberOfLines */ - private function addNewLines(&$content, $numberOfLines = 1) + private function addNewLines(string &$content, int $numberOfLines = 1): void { while ($numberOfLines > 0) { $content .= $this->newLineCharacter; @@ -362,11 +328,8 @@ private function addNewLines(&$content, $numberOfLines = 1) /** * Adds indentation to the passed content reference. - * - * @param string $content - * @param int $numberOfIndents */ - private function addIndent(&$content, $numberOfIndents = 1) + private function addIndent(string &$content, int $numberOfIndents = 1): void { while ($numberOfIndents > 0) { $content .= $this->indentCharacter; @@ -376,39 +339,37 @@ private function addIndent(&$content, $numberOfIndents = 1) /** * Cleans the iSeed section - * @return bool + * + * @throws FileNotFoundException */ - public function cleanSection() + public function cleanSection(): bool { - $databaseSeederPath = base_path() . config('iseed::config.path') . '/DatabaseSeeder.php'; + $databaseSeederPath = base_path().config('iseed::config.path').'/DatabaseSeeder.php'; $content = $this->files->get($databaseSeederPath); - $content = preg_replace("/(\#iseed_start.+?)\#iseed_end/us", "#iseed_start\n\t\t#iseed_end", $content); + $content = preg_replace("/(\#iseed_start.+?)\#iseed_end/us", "#iseed_start\n\t\t#iseed_end", (string) $content); return $this->files->put($databaseSeederPath, $content) !== false; - return false; } /** * Updates the DatabaseSeeder file's run method (kudoz to: https://github.com/JeffreyWay/Laravel-4-Generators) - * @param string $className - * @return bool */ - public function updateDatabaseSeederRunMethod($className) + public function updateDatabaseSeederRunMethod(string $className): bool { - $databaseSeederPath = base_path() . config('iseed::config.path') . '/DatabaseSeeder.php'; + $databaseSeederPath = base_path().config('iseed::config.path').'/DatabaseSeeder.php'; - $content = $this->files->get($databaseSeederPath); - if (strpos($content, "\$this->call({$className}::class)") === false) { + $content = (string) $this->files->get($databaseSeederPath); + if (! str_contains((string) $content, "\$this->call({$className}::class)")) { if ( strpos($content, '#iseed_start') && strpos($content, '#iseed_end') && strpos($content, '#iseed_start') < strpos($content, '#iseed_end') ) { - $content = preg_replace("/(\#iseed_start.+?)(\#iseed_end)/us", "$1\$this->call({$className}::class);{$this->newLineCharacter}{$this->indentCharacter}{$this->indentCharacter}$2", $content); + $content = preg_replace("/(\#iseed_start.+?)(\#iseed_end)/us", "$1\$this->call({$className}::class);{$this->newLineCharacter}{$this->indentCharacter}{$this->indentCharacter}$2", (string) $content); } else { - $content = preg_replace("/(run\(\).+?)}/us", "$1{$this->indentCharacter}\$this->call({$className}::class);{$this->newLineCharacter}{$this->indentCharacter}}", $content); + $content = preg_replace("/(run\(\).+?)}/us", "$1{$this->indentCharacter}\$this->call({$className}::class);{$this->newLineCharacter}{$this->indentCharacter}}", (string) $content); } } diff --git a/src/Orangehill/Iseed/IseedCommand.php b/src/Orangehill/Iseed/IseedCommand.php index 58517ec..1e7cb97 100755 --- a/src/Orangehill/Iseed/IseedCommand.php +++ b/src/Orangehill/Iseed/IseedCommand.php @@ -3,6 +3,9 @@ namespace Orangehill\Iseed; use Illuminate\Console\Command; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Schema; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -22,46 +25,34 @@ class IseedCommand extends Command */ protected $description = 'Generate seed file from table'; - /** - * Create a new command instance. - * - * @return \Orangehill\Iseed\IseedCommand - */ - public function __construct() - { - parent::__construct(); - } - /** * Execute the console command. - * - * @return void */ - public function handle() + public function handle(): int { - return $this->fire(); + $this->fire(); + + return 1; } /** * Execute the console command (for <= 5.4). - * - * @return void */ - public function fire() + public function fire(): void { // if clean option is checked empty iSeed template in DatabaseSeeder.php if ($this->option('clean')) { app('iseed')->cleanSection(); } - $tables = explode(",", $this->argument('tables')); + $tables = explode(',', $this->argument('tables')); $max = intval($this->option('max')); $chunkSize = intval($this->option('chunksize')); - $exclude = explode(",", $this->option('exclude')); - $prerunEvents = explode(",", $this->option('prerun')); - $postrunEvents = explode(",", $this->option('postrun')); + $exclude = explode(',', $this->option('exclude')); + $prerunEvents = explode(',', $this->option('prerun')); + $postrunEvents = explode(',', $this->option('postrun')); $dumpAuto = intval($this->option('dumpauto')); - $indexed = !$this->option('noindex'); + $indexed = ! $this->option('noindex'); $orderBy = $this->option('orderby'); $direction = $this->option('direction'); $prefix = $this->option('classnameprefix'); @@ -89,10 +80,10 @@ public function fire() $tableIncrement++; // generate file and class name based on name of the table - list($fileName, $className) = $this->generateFileName($table, $prefix, $suffix); + [$fileName, $className] = $this->generateFileName($table, $prefix, $suffix); // if file does not exist or force option is turned on generate seeder - if (!\File::exists($fileName) || $this->option('force')) { + if (! File::exists($fileName) || $this->option('force')) { $this->printResult( app('iseed')->generateSeed( $table, @@ -111,10 +102,11 @@ public function fire() ), $table ); + continue; } - if ($this->confirm('File ' . $className . ' already exist. Do you wish to override it? [yes|no]')) { + if ($this->confirm('File '.$className.' already exist. Do you wish to override it? [yes|no]')) { // if user said yes overwrite old seeder $this->printResult( app('iseed')->generateSeed( @@ -134,58 +126,52 @@ public function fire() ); } } - - return; } /** * Get the console command arguments. - * - * @return array */ - protected function getArguments() + protected function getArguments(): array { - return array( - array('tables', InputArgument::REQUIRED, 'comma separated string of table names'), - ); + return [ + ['tables', InputArgument::REQUIRED, 'comma separated string of table names'], + ]; } /** * Get the console command options. - * - * @return array */ - protected function getOptions() + protected function getOptions(): array { - return array( - array('clean', null, InputOption::VALUE_NONE, 'clean iseed section', null), - array('force', null, InputOption::VALUE_NONE, 'force overwrite of all existing seed classes', null), - array('database', null, InputOption::VALUE_OPTIONAL, 'database connection', \Config::get('database.default')), - array('max', null, InputOption::VALUE_OPTIONAL, 'max number of rows', null), - array('chunksize', null, InputOption::VALUE_OPTIONAL, 'size of data chunks for each insert query', null), - array('exclude', null, InputOption::VALUE_OPTIONAL, 'exclude columns', null), - array('prerun', null, InputOption::VALUE_OPTIONAL, 'prerun event name', null), - array('postrun', null, InputOption::VALUE_OPTIONAL, 'postrun event name', null), - array('dumpauto', null, InputOption::VALUE_OPTIONAL, 'run composer dump-autoload', true), - array('noindex', null, InputOption::VALUE_NONE, 'no indexing in the seed', null), - array('orderby', null, InputOption::VALUE_OPTIONAL, 'orderby desc by column', null), - array('direction', null, InputOption::VALUE_OPTIONAL, 'orderby direction', null), - array('classnameprefix', null, InputOption::VALUE_OPTIONAL, 'prefix for class and file name', null), - array('classnamesuffix', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', null), - ); + return [ + ['clean', null, InputOption::VALUE_NONE, 'clean iseed section', null], + ['force', null, InputOption::VALUE_NONE, 'force overwrite of all existing seed classes', null], + ['database', null, InputOption::VALUE_OPTIONAL, 'database connection', Config::get('database.default')], + ['max', null, InputOption::VALUE_OPTIONAL, 'max number of rows', null], + ['chunksize', null, InputOption::VALUE_OPTIONAL, 'size of data chunks for each insert query', null], + ['exclude', null, InputOption::VALUE_OPTIONAL, 'exclude columns', null], + ['prerun', null, InputOption::VALUE_OPTIONAL, 'prerun event name', null], + ['postrun', null, InputOption::VALUE_OPTIONAL, 'postrun event name', null], + ['dumpauto', null, InputOption::VALUE_OPTIONAL, 'run composer dump-autoload', true], + ['noindex', null, InputOption::VALUE_NONE, 'no indexing in the seed', null], + ['orderby', null, InputOption::VALUE_OPTIONAL, 'orderby desc by column', null], + ['direction', null, InputOption::VALUE_OPTIONAL, 'orderby direction', null], + ['classnameprefix', null, InputOption::VALUE_OPTIONAL, 'prefix for class and file name', null], + ['classnamesuffix', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', null], + ]; } /** * Provide user feedback, based on success or not. * - * @param boolean $successful - * @param string $table + * @param bool $successful * @return void */ - protected function printResult($successful, $table) + protected function printResult(bool|int $successful, string $table) { if ($successful) { $this->info("Created a seed file from table {$table}"); + return; } @@ -195,18 +181,19 @@ protected function printResult($successful, $table) /** * Generate file name, to be used in test wether seed file already exist * - * @param string $table + * * @return string */ - protected function generateFileName($table, $prefix=null, $suffix=null) + protected function generateFileName(string $table, ?string $prefix = null, ?string $suffix = null): array|string { - if (!\Schema::connection($this->option('database') ? $this->option('database') : config('database.default'))->hasTable($table)) { + if (! Schema::connection($this->option('database') ?: config('database.default'))->hasTable($table)) { throw new TableNotFoundException("Table $table was not found."); } // Generate class name and file name $className = app('iseed')->generateClassName($table, $prefix, $suffix); - $seedPath = base_path() . config('iseed::config.path'); - return [$seedPath . '/' . $className . '.php', $className . '.php']; + $seedPath = base_path().config('iseed::config.path'); + + return [$seedPath.'/'.$className.'.php', $className.'.php']; } } diff --git a/src/Orangehill/Iseed/IseedServiceProvider.php b/src/Orangehill/Iseed/IseedServiceProvider.php index b3dfea8..2032eb1 100644 --- a/src/Orangehill/Iseed/IseedServiceProvider.php +++ b/src/Orangehill/Iseed/IseedServiceProvider.php @@ -1,79 +1,65 @@ -registerResources(); - - $this->app->singleton('iseed', function($app) { - return new Iseed; - }); - - $this->app->booting(function() { - $loader = \Illuminate\Foundation\AliasLoader::getInstance(); - $loader->alias('Iseed', 'Orangehill\Iseed\Facades\Iseed'); - }); - - $this->app->singleton('command.iseed', function($app) { - return new IseedCommand; - }); - - $this->commands('command.iseed'); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array('iseed'); - } - - /** - * Register the package resources. - * - * @return void - */ - protected function registerResources() - { - $userConfigFile = app()->configPath().'/iseed.php'; - $packageConfigFile = __DIR__.'/../../config/config.php'; - $config = $this->app['files']->getRequire($packageConfigFile); - - if (file_exists($userConfigFile)) { - $userConfig = $this->app['files']->getRequire($userConfigFile); - $config = array_replace_recursive($config, $userConfig); - } - - $this->app['config']->set('iseed::config', $config); - } -} \ No newline at end of file +registerResources(); + + $this->app->singleton('iseed', fn ($app) => new Iseed); + + $this->app->booting(function () { + $loader = \Illuminate\Foundation\AliasLoader::getInstance(); + $loader->alias('Iseed', \Orangehill\Iseed\Facades\Iseed::class); + }); + + $this->app->singleton('command.iseed', fn ($app) => new IseedCommand); + + $this->commands('command.iseed'); + } + + /** + * Get the services provided by the provider. + */ + public function provides(): array + { + return ['iseed']; + } + + /** + * Register the package resources. + */ + protected function registerResources(): void + { + $userConfigFile = app()->configPath().'/iseed.php'; + $packageConfigFile = __DIR__.'/../../config/config.php'; + $config = $this->app['files']->getRequire($packageConfigFile); + + if (file_exists($userConfigFile)) { + $userConfig = $this->app['files']->getRequire($userConfigFile); + $config = array_replace_recursive($config, $userConfig); + } + + $this->app['config']->set('iseed::config', $config); + } +} diff --git a/src/config/config.php b/src/config/config.php index 9720983..bc8689d 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -1,5 +1,6 @@ '/database/seeders', - 'chunk_size' => 500 // Maximum number of rows per insert statement -); + 'chunk_size' => 500, // Maximum number of rows per insert statement +]; diff --git a/tests/IseedTest.php b/tests/IseedTest.php index 5165adb..597141e 100644 --- a/tests/IseedTest.php +++ b/tests/IseedTest.php @@ -1,4 +1,5 @@ $stub) { $output = $iSeed->populateStub('test_class', $productionStub, 'test_table', $stub['data'], 500); - $this->assertEquals($stub['content'], $output, "Stub {$key} is not what it's expected to be."); + $this->assertEqualsCanonicalizing($stub['content'], $output, "Stub {$key} is not what it's expected to be."); } }