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
+1
View File
@@ -21,6 +21,7 @@
], ],
"require": { "require": {
"php": ">=7.2", "php": ">=7.2",
"ext-json": "*",
"barryvdh/reflection-docblock": "^2.0.6", "barryvdh/reflection-docblock": "^2.0.6",
"composer/composer": "^1.6 || ^2.0@dev", "composer/composer": "^1.6 || ^2.0@dev",
"doctrine/dbal": "~2.3", "doctrine/dbal": "~2.3",
+18 -21
View File
@@ -63,12 +63,12 @@ class Alias
$this->detectRoot(); $this->detectRoot();
if ((!$this->isTrait() && $this->root)) { if (!$this->root || $this->isTrait()) {
$this->valid = true;
} else {
return; return;
} }
$this->valid = true;
$this->addClass($this->root); $this->addClass($this->root);
$this->detectFake(); $this->detectFake();
$this->detectNamespace(); $this->detectNamespace();
@@ -306,10 +306,7 @@ class Alias
protected function isTrait() protected function isTrait()
{ {
// Check if the facade is not a Trait // Check if the facade is not a Trait
if (function_exists('trait_exists') && trait_exists($this->facade)) { return trait_exists($this->facade);
return true;
}
return false;
} }
/** /**
@@ -416,22 +413,22 @@ class Alias
{ {
$serializer = new DocBlockSerializer(1, $prefix); $serializer = new DocBlockSerializer(1, $prefix);
if ($this->phpdoc) { if (!$this->phpdoc) {
if ($this->config->get('ide-helper.include_class_docblocks')) { return '';
// if a class doesn't expose any DocBlock tags
// we can perform reflection on the class and
// add in the original class DocBlock
if (count($this->phpdoc->getTags()) === 0) {
$class = new ReflectionClass($this->root);
$this->phpdoc = new DocBlock($class->getDocComment());
}
}
$this->removeDuplicateMethodsFromPhpDoc();
return $serializer->getDocComment($this->phpdoc);
} }
return ''; if ($this->config->get('ide-helper.include_class_docblocks')) {
// if a class doesn't expose any DocBlock tags
// we can perform reflection on the class and
// add in the original class DocBlock
if (count($this->phpdoc->getTags()) === 0) {
$class = new ReflectionClass($this->root);
$this->phpdoc = new DocBlock($class->getDocComment());
}
}
$this->removeDuplicateMethodsFromPhpDoc();
return $serializer->getDocComment($this->phpdoc);
} }
/** /**
+35 -34
View File
@@ -85,46 +85,47 @@ class GeneratorCommand extends Command
$this->error( $this->error(
'Error generating IDE Helper: first delete your compiled file (php artisan clear-compiled)' '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 { } 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 = ''; $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); $generator = new Generator($this->config, $this->view, $this->getOutput(), $helpers);
$content = $generator->generate($format); $content = $generator->generate($format);
$written = $this->files->put($filename, $content); $written = $this->files->put($filename, $content);
if ($written !== false) { if ($written !== false) {
$this->info("A new helper file was written to $filename"); $this->info("A new helper file was written to $filename");
if ($this->option('write_mixins')) { if ($this->option('write_mixins')) {
Eloquent::writeEloquentModelHelper($this, $this->files); Eloquent::writeEloquentModelHelper($this, $this->files);
}
} else {
$this->error("The helper file could not be created at $filename");
} }
} 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 = []; 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. * determine the actual date class and store it here.
* *
* @var string * @var string
@@ -224,9 +224,7 @@ class ModelsCommand extends Command
continue; continue;
} }
if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $this->comment("Loading model '$name'", OutputInterface::VERBOSITY_VERBOSE);
$this->comment("Loading model '$name'");
}
if (!$reflectionClass->IsInstantiable()) { if (!$reflectionClass->IsInstantiable()) {
// ignore abstract class or interface // ignore abstract class or interface
@@ -346,20 +344,20 @@ class ModelsCommand extends Command
if (!isset($this->properties[$name])) { if (!isset($this->properties[$name])) {
continue; continue;
} else { }
$realType = $this->checkForCustomLaravelCasts($realType);
$realType = $this->getTypeOverride($realType);
$this->properties[$name]['type'] = $this->getTypeInModel($model, $realType);
if (isset($this->nullableColumns[$name])) { $realType = $this->checkForCustomLaravelCasts($realType);
$this->properties[$name]['type'] .= '|null'; $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 * @param string $type
* @return string|null * @return string|null
@@ -368,7 +366,7 @@ class ModelsCommand extends Command
{ {
$typeOverrides = $this->laravel['config']->get('ide-helper.type_overrides', array()); $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); $columns = $schema->listTableColumns($table, $database);
if ($columns) { if (!$columns) {
foreach ($columns as $column) { return;
$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;
}
}
$comment = $column->getComment(); foreach ($columns as $column) {
if (!$column->getNotnull()) { $name = $column->getName();
$this->nullableColumns[$name] = true; 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), $comment = $column->getComment();
true, if (!$column->getNotnull()) {
true, $this->nullableColumns[$name] = true;
$comment, }
!$column->getNotnull() $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) public function getParameters($method)
{ {
//Loop through the default values for paremeters, and make the correct output string //Loop through the default values for parameters, and make the correct output string
$params = array();
$paramsWithDefault = array(); $paramsWithDefault = array();
/** @var \ReflectionParameter $param */ /** @var \ReflectionParameter $param */
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
$paramClass = $param->getClass(); $paramClass = $param->getClass();
$paramStr = (!is_null($paramClass) ? '\\' . $paramClass->getName() . ' ' : '') . '$' . $param->getName(); $paramStr = (!is_null($paramClass) ? '\\' . $paramClass->getName() . ' ' : '') . '$' . $param->getName();
$params[] = $paramStr;
if ($param->isOptional() && $param->isDefaultValueAvailable()) { if ($param->isOptional() && $param->isDefaultValueAvailable()) {
$default = $param->getDefaultValue(); $default = $param->getDefaultValue();
if (is_bool($default)) { if (is_bool($default)) {
@@ -1046,11 +1044,6 @@ class ModelsCommand extends Command
return $type; return $type;
} }
/**
* @param object|ReflectionClass $model
* @param string $type
* @return string
*/
protected function getTypeInModel(object $model, ?string $type): ?string protected function getTypeInModel(object $model, ?string $type): ?string
{ {
if ($type === null) { if ($type === null) {
@@ -1064,11 +1057,6 @@ class ModelsCommand extends Command
return $type; return $type;
} }
/**
* @param object|ReflectionClass $model
* @param string $className
* @return string
*/
protected function getClassNameInDestinationFile(object $model, string $className): string protected function getClassNameInDestinationFile(object $model, string $className): string
{ {
$reflection = $model instanceof ReflectionClass $reflection = $model instanceof ReflectionClass
+22 -18
View File
@@ -85,25 +85,29 @@ class Eloquent
} }
$filename = $reflection->getFileName(); $filename = $reflection->getFileName();
if ($filename) { if (!$filename) {
$contents = $files->get($filename);
if ($contents) {
$count = 0;
$contents = str_replace($originalDoc, $docComment, $contents, $count);
if ($count > 0) {
if ($files->put($filename, $contents)) {
$command->info('Wrote expected docblock to ' . $filename);
} else {
$command->error('File write failed to ' . $filename);
}
} else {
$command->error('Content did not change ' . $contents);
}
} else {
$command->error('No file contents found ' . $filename);
}
} else {
$command->error('Filename not found ' . $class); $command->error('Filename not found ' . $class);
return;
} }
$contents = $files->get($filename);
if (!$contents) {
$command->error('No file contents found ' . $filename);
return;
}
$count = 0;
$contents = str_replace($originalDoc, $docComment, $contents, $count);
if ($count <= 0) {
$command->error('Content did not change ' . $contents);
return;
}
if (!$files->put($filename, $contents)) {
$command->error('File write failed to ' . $filename);
return;
}
$command->info('Wrote expected docblock to ' . $filename);
} }
} }
-1
View File
@@ -3,7 +3,6 @@
namespace Barryvdh\LaravelIdeHelper; namespace Barryvdh\LaravelIdeHelper;
use Barryvdh\Reflection\DocBlock; use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Tag;
class Macro extends Method class Macro extends Method
{ {
+28 -26
View File
@@ -251,35 +251,37 @@ class Method
*/ */
protected function normalizeReturn(DocBlock $phpdoc) protected function normalizeReturn(DocBlock $phpdoc)
{ {
//Get the return type and adjust them for beter autocomplete //Get the return type and adjust them for better autocomplete
$returnTags = $phpdoc->getTagsByName('return'); $returnTags = $phpdoc->getTagsByName('return');
if ($returnTags) {
/** @var ReturnTag $tag */
$tag = reset($returnTags);
// Get the expanded type
$returnValue = $tag->getType();
// Replace the interfaces if (count($returnTags) === 0) {
foreach ($this->interfaces as $interface => $real) {
$returnValue = str_replace($interface, $real, $returnValue);
}
// Set the changed content
$tag->setContent($returnValue . ' ' . $tag->getDescription());
$this->return = $returnValue;
if ($tag->getType() === '$this') {
Str::contains($this->root, Builder::class)
? $tag->setType($this->root . '|static')
: $tag->setType($this->root);
}
} else {
$this->return = null; $this->return = null;
return;
}
/** @var ReturnTag $tag */
$tag = reset($returnTags);
// Get the expanded type
$returnValue = $tag->getType();
// Replace the interfaces
foreach ($this->interfaces as $interface => $real) {
$returnValue = str_replace($interface, $real, $returnValue);
}
// Set the changed content
$tag->setContent($returnValue . ' ' . $tag->getDescription());
$this->return = $returnValue;
if ($tag->getType() === '$this') {
Str::contains($this->root, Builder::class)
? $tag->setType($this->root . '|static')
: $tag->setType($this->root);
} }
} }
/** /**
* Convert keywwords that are incorrect. * Convert keywords that are incorrect.
* *
* @param string $string * @param string $string
* @return string * @return string
@@ -311,11 +313,11 @@ class Method
* Get the parameters and format them correctly * Get the parameters and format them correctly
* *
* @param \ReflectionMethod $method * @param \ReflectionMethod $method
* @return array * @return void
*/ */
public function getParameters($method) public function getParameters($method)
{ {
//Loop through the default values for paremeters, and make the correct output string //Loop through the default values for parameters, and make the correct output string
$params = array(); $params = array();
$paramsWithDefault = array(); $paramsWithDefault = array();
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
@@ -366,9 +368,9 @@ class Method
if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) { if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) {
//Not at the end yet, try another parent/interface.. //Not at the end yet, try another parent/interface..
return $this->getInheritDoc($method); return $this->getInheritDoc($method);
} else {
return $phpdoc;
} }
return $phpdoc;
} }
} }
} }