diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 2f46b64..6b655e7 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -12,6 +12,7 @@ namespace Barryvdh\LaravelIdeHelper\Console; use Illuminate\Console\Command; use Illuminate\Support\Str; +use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; @@ -28,6 +29,10 @@ use phpDocumentor\Reflection\DocBlock\Serializer as DocBlockSerializer; */ class ModelsCommand extends Command { + /** + * @var Filesystem $files + */ + protected $files; /** * The console command name. @@ -50,7 +55,14 @@ class ModelsCommand extends Command protected $dirs = array(); protected $reset; - + /** + * @param Filesystem $files + */ + public function __construct(Filesystem $files) + { + parent::__construct(); + $this->files = $files; + } /** * Execute the console command. * @@ -81,7 +93,7 @@ class ModelsCommand extends Command $content = $this->generateDocs($model, $ignore); if (!$this->write) { - $written = \File::put($filename, $content); + $written = $this->files->put($filename, $content); if ($written !== false) { $this->info("Model information was written to $filename"); } else { @@ -181,7 +193,6 @@ class ModelsCommand extends Command $this->getPropertiesFromMethods($model); $output .= $this->createPhpDocs($name); - $ignore[] = $name; } catch (\Exception $e) { $this->error("Exception: " . $e->getMessage() . "\nCould not analyze class $name."); } @@ -336,7 +347,6 @@ class ModelsCommand extends Command $code .= $file->current(); $file->next(); } - $code = trim(preg_replace('/\s\s+/', '', $code)); $begin = strpos($code, 'function('); $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); @@ -352,29 +362,24 @@ class ModelsCommand extends Command $search = '$this->' . $relation . '('; if ($pos = stripos($code, $search)) { $code = substr($code, $pos + strlen($search)); - $end = strpos($code, ')->') ?:strpos($code, ');'); - if (false !== $end) { - $arguments = substr($code, 0, $end); - $arguments = array_map(function($item) { - return trim($item, ' \'\"'); - }, explode(',', $arguments)); - //Remove quotes, ensure 1 \ in front of the model - $returnModel = $this->getClassName($arguments[0], $model); - if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') { - //Collection or array of models (because Collection is Arrayable) - $this->setProperty( - $method, - '\Illuminate\Database\Eloquent\Collection|' . $returnModel . '[]', - true, - null - ); - } else { - //Single model is returned - $this->setProperty($method, $returnModel, true, null); - } + $arguments = explode(',', substr($code, 0, strpos($code, ')'))); + //Remove quotes, ensure 1 \ in front of the model + $returnModel = $this->getClassName($arguments[0], $model); + if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') { + //Collection or array of models (because Collection is Arrayable) + $this->setProperty( + $method, + '\Illuminate\Database\Eloquent\Collection|' . $returnModel . '[]', + true, + null + ); + } else { + //Single model is returned + $this->setProperty($method, $returnModel, true, null); } } } + } } } @@ -481,7 +486,7 @@ class ModelsCommand extends Command if ($this->write) { $filename = $reflection->getFileName(); - $contents = \File::get($filename); + $contents = $this->files->get($filename); if ($originalDoc) { $contents = str_replace($originalDoc, $docComment, $contents); } else { @@ -492,7 +497,7 @@ class ModelsCommand extends Command $contents = substr_replace($contents, $replace, $pos, strlen($needle)); } } - if (\File::put($filename, $contents)) { + if ($this->files->put($filename, $contents)) { $this->info('Written new phpDocBlock to ' . $filename); } } @@ -544,7 +549,7 @@ class ModelsCommand extends Command { // If the class name was resolved via get_class($this) or static::class if (strpos($className, 'get_class($this)') !== false || strpos($className, 'static::class') !== false) { - return "\\" . get_class($model); + return get_class($model); } // If the class name was resolved via ::class (PHP 5.5+)