Fix typos; Optimise if-else branches (#1009)

This commit is contained in:
Danny van der Sluijs
2020-08-28 09:15:43 +02:00
committed by GitHub
parent 1d9e82f2b7
commit 8287f02afa
7 changed files with 181 additions and 189 deletions
+35 -34
View File
@@ -85,46 +85,47 @@ class GeneratorCommand extends Command
$this->error(
'Error generating IDE Helper: first delete your compiled file (php artisan clear-compiled)'
);
return;
}
$filename = $this->argument('filename');
$format = $this->option('format');
// Strip the php extension
if (substr($filename, -4, 4) === '.php') {
$filename = substr($filename, 0, -4);
}
$filename .= '.' . $format;
if ($this->option('memory')) {
$this->useMemoryDriver();
}
$helpers = '';
if ($this->option('helpers') || ($this->config->get('ide-helper.include_helpers'))) {
foreach ($this->config->get('ide-helper.helper_files', array()) as $helper) {
if (file_exists($helper)) {
$helpers .= str_replace(array('<?php', '?>'), '', $this->files->get($helper));
}
}
} else {
$filename = $this->argument('filename');
$format = $this->option('format');
// Strip the php extension
if (substr($filename, -4, 4) == '.php') {
$filename = substr($filename, 0, -4);
}
$filename .= '.' . $format;
if ($this->option('memory')) {
$this->useMemoryDriver();
}
$helpers = '';
if ($this->option('helpers') || ($this->config->get('ide-helper.include_helpers'))) {
foreach ($this->config->get('ide-helper.helper_files', array()) as $helper) {
if (file_exists($helper)) {
$helpers .= str_replace(array('<?php', '?>'), '', $this->files->get($helper));
}
}
} else {
$helpers = '';
}
}
$generator = new Generator($this->config, $this->view, $this->getOutput(), $helpers);
$content = $generator->generate($format);
$written = $this->files->put($filename, $content);
$generator = new Generator($this->config, $this->view, $this->getOutput(), $helpers);
$content = $generator->generate($format);
$written = $this->files->put($filename, $content);
if ($written !== false) {
$this->info("A new helper file was written to $filename");
if ($written !== false) {
$this->info("A new helper file was written to $filename");
if ($this->option('write_mixins')) {
Eloquent::writeEloquentModelHelper($this, $this->files);
}
} else {
$this->error("The helper file could not be created at $filename");
if ($this->option('write_mixins')) {
Eloquent::writeEloquentModelHelper($this, $this->files);
}
} else {
$this->error("The helper file could not be created at $filename");
}
}
+75 -87
View File
@@ -71,7 +71,7 @@ class ModelsCommand extends Command
protected $nullableColumns = [];
/**
* During initializtion we use Laravels Date Facade to
* During initialization we use Laravels Date Facade to
* determine the actual date class and store it here.
*
* @var string
@@ -224,9 +224,7 @@ class ModelsCommand extends Command
continue;
}
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$this->comment("Loading model '$name'");
}
$this->comment("Loading model '$name'", OutputInterface::VERBOSITY_VERBOSE);
if (!$reflectionClass->IsInstantiable()) {
// ignore abstract class or interface
@@ -346,20 +344,20 @@ class ModelsCommand extends Command
if (!isset($this->properties[$name])) {
continue;
} else {
$realType = $this->checkForCustomLaravelCasts($realType);
$realType = $this->getTypeOverride($realType);
$this->properties[$name]['type'] = $this->getTypeInModel($model, $realType);
}
if (isset($this->nullableColumns[$name])) {
$this->properties[$name]['type'] .= '|null';
}
$realType = $this->checkForCustomLaravelCasts($realType);
$realType = $this->getTypeOverride($realType);
$this->properties[$name]['type'] = $this->getTypeInModel($model, $realType);
if (isset($this->nullableColumns[$name])) {
$this->properties[$name]['type'] .= '|null';
}
}
}
/**
* Returns the overide type for the give type.
* Returns the override type for the give type.
*
* @param string $type
* @return string|null
@@ -368,7 +366,7 @@ class ModelsCommand extends Command
{
$typeOverrides = $this->laravel['config']->get('ide-helper.type_overrides', array());
return isset($typeOverrides[$type]) ? $typeOverrides[$type] : $type;
return $typeOverrides[$type] ?? $type;
}
/**
@@ -396,70 +394,72 @@ class ModelsCommand extends Command
$columns = $schema->listTableColumns($table, $database);
if ($columns) {
foreach ($columns as $column) {
$name = $column->getName();
if (in_array($name, $model->getDates())) {
$type = $this->dateClass;
} else {
$type = $column->getType()->getName();
switch ($type) {
case 'string':
case 'text':
case 'date':
case 'time':
case 'guid':
case 'datetimetz':
case 'datetime':
case 'decimal':
$type = 'string';
break;
case 'integer':
case 'bigint':
case 'smallint':
$type = 'integer';
break;
case 'boolean':
switch (config('database.default')) {
case 'sqlite':
case 'mysql':
$type = 'integer';
break;
default:
$type = 'boolean';
break;
}
break;
case 'float':
$type = 'float';
break;
default:
$type = 'mixed';
break;
}
}
if (!$columns) {
return;
}
$comment = $column->getComment();
if (!$column->getNotnull()) {
$this->nullableColumns[$name] = true;
foreach ($columns as $column) {
$name = $column->getName();
if (in_array($name, $model->getDates())) {
$type = $this->dateClass;
} else {
$type = $column->getType()->getName();
switch ($type) {
case 'string':
case 'text':
case 'date':
case 'time':
case 'guid':
case 'datetimetz':
case 'datetime':
case 'decimal':
$type = 'string';
break;
case 'integer':
case 'bigint':
case 'smallint':
$type = 'integer';
break;
case 'boolean':
switch (config('database.default')) {
case 'sqlite':
case 'mysql':
$type = 'integer';
break;
default:
$type = 'boolean';
break;
}
break;
case 'float':
$type = 'float';
break;
default:
$type = 'mixed';
break;
}
$this->setProperty(
$name,
$this->getTypeInModel($model, $type),
true,
true,
$comment,
!$column->getNotnull()
}
$comment = $column->getComment();
if (!$column->getNotnull()) {
$this->nullableColumns[$name] = true;
}
$this->setProperty(
$name,
$this->getTypeInModel($model, $type),
true,
true,
$comment,
!$column->getNotnull()
);
if ($this->write_model_magic_where) {
$this->setMethod(
Str::camel("where_" . $name),
$this->getClassNameInDestinationFile($model, \Illuminate\Database\Eloquent\Builder::class)
. '|'
. $this->getClassNameInDestinationFile($model, get_class($model)),
array('$value')
);
if ($this->write_model_magic_where) {
$this->setMethod(
Str::camel("where_" . $name),
$this->getClassNameInDestinationFile($model, \Illuminate\Database\Eloquent\Builder::class)
. '|'
. $this->getClassNameInDestinationFile($model, get_class($model)),
array('$value')
);
}
}
}
}
@@ -856,14 +856,12 @@ class ModelsCommand extends Command
*/
public function getParameters($method)
{
//Loop through the default values for paremeters, and make the correct output string
$params = array();
//Loop through the default values for parameters, and make the correct output string
$paramsWithDefault = array();
/** @var \ReflectionParameter $param */
foreach ($method->getParameters() as $param) {
$paramClass = $param->getClass();
$paramStr = (!is_null($paramClass) ? '\\' . $paramClass->getName() . ' ' : '') . '$' . $param->getName();
$params[] = $paramStr;
if ($param->isOptional() && $param->isDefaultValueAvailable()) {
$default = $param->getDefaultValue();
if (is_bool($default)) {
@@ -1046,11 +1044,6 @@ class ModelsCommand extends Command
return $type;
}
/**
* @param object|ReflectionClass $model
* @param string $type
* @return string
*/
protected function getTypeInModel(object $model, ?string $type): ?string
{
if ($type === null) {
@@ -1064,11 +1057,6 @@ class ModelsCommand extends Command
return $type;
}
/**
* @param object|ReflectionClass $model
* @param string $className
* @return string
*/
protected function getClassNameInDestinationFile(object $model, string $className): string
{
$reflection = $model instanceof ReflectionClass