From 463a6b31bd7be4666a5d2a1f924a7e27815edd55 Mon Sep 17 00:00:00 2001 From: James King Date: Mon, 6 Jan 2020 17:14:29 +0000 Subject: [PATCH 1/2] Stop calling a method static if its not static --- src/Method.php | 5 +---- tests/MethodTest.php | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/Method.php b/src/Method.php index 028e1b766..726ef2236 100644 --- a/src/Method.php +++ b/src/Method.php @@ -67,9 +67,6 @@ public function __construct($method, $alias, $class, $methodName = null, $interf //Get the parameters, including formatted default values $this->getParameters($method); - - //Make the method static - $this->phpdoc->appendTag(Tag::createInstance('@static', $this->phpdoc)); } /** @@ -357,7 +354,7 @@ protected function getInheritDoc($reflectionMethod) if ($method) { $namespace = $method->getDeclaringClass()->getNamespaceName(); $phpdoc = new DocBlock($method, new Context($namespace)); - + if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) { //Not at the end yet, try another parent/interface.. return $this->getInheritDoc($method); diff --git a/tests/MethodTest.php b/tests/MethodTest.php index f077ffb14..93bf374f8 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -36,7 +36,6 @@ public function testOutput() * @param string $last * @param string $first * @param string $middle - * @static */'; $this->assertEquals($output, $method->getDocComment('')); $this->assertEquals('setName', $method->getName()); @@ -47,6 +46,34 @@ public function testOutput() $this->assertEquals(['$last', '$first = \'Barry\'', '...$middle'], $method->getParamsWithDefault(false)); $this->assertEquals(true, $method->shouldReturn()); } + + /** + * Test the output of a class (static method) + */ + public function testStaticOutput() + { + $reflectionClass = new \ReflectionClass(ExampleStaticClass::class); + $reflectionMethod = $reflectionClass->getMethod('setName'); + + $method = new Method($reflectionMethod, 'Example', $reflectionClass); + + $output = '/** + * + * + * @param string $last + * @param string $first + * @param string $middle + * @static + */'; + $this->assertEquals($output, $method->getDocComment('')); + $this->assertEquals('setName', $method->getName()); + $this->assertEquals('\\'.ExampleStaticClass::class, $method->getDeclaringClass()); + $this->assertEquals('$last, $first, ...$middle', $method->getParams(true)); + $this->assertEquals(['$last', '$first', '...$middle'], $method->getParams(false)); + $this->assertEquals('$last, $first = \'Barry\', ...$middle', $method->getParamsWithDefault(true)); + $this->assertEquals(['$last', '$first = \'Barry\'', '...$middle'], $method->getParamsWithDefault(false)); + $this->assertEquals(true, $method->shouldReturn()); + } } class ExampleClass @@ -61,3 +88,17 @@ public function setName($last, $first = 'Barry', ...$middle) return; } } + +class ExampleStaticClass +{ + /** + * @param string $last + * @param string $first + * @param string $middle + * @static + */ + public static function setName($last, $first = 'Barry', ...$middle) + { + return; + } +} From 6f756ea9727af0108e9a9e5f5aa9e81878360731 Mon Sep 17 00:00:00 2001 From: James King Date: Mon, 6 Jan 2020 20:08:44 +0000 Subject: [PATCH 2/2] More static detection --- .editorconfig | 15 ---------- src/Console/ModelsCommand.php | 55 ++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 26 deletions(-) delete mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 9d4990946..000000000 --- a/.editorconfig +++ /dev/null @@ -1,15 +0,0 @@ -; This file is for unifying the coding style for different editors and IDEs. -; More information at http://editorconfig.org - -root = true - -[*] -charset = utf-8 -indent_size = 4 -indent_style = space -end_of_line = lf -insert_final_newline = true -trim_trailing_whitespace = true - -[*.md] -trim_trailing_whitespace = false \ No newline at end of file diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index ca2bfb891..1e3f57f90 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -405,10 +405,12 @@ protected function getPropertiesFromTable($model) } $this->setProperty($name, $type, true, true, $comment, !$column->getNotnull()); if ($this->write_model_magic_where) { + $method = Str::camel("where_" . $name); $this->setMethod( - Str::camel("where_" . $name), + $method, '\Illuminate\Database\Eloquent\Builder|\\' . get_class($model), - array('$value') + array('$value'), + (new \ReflectionMethod($model, $method))->isStatic() ); } } @@ -454,14 +456,15 @@ protected function getPropertiesFromMethods($model) $args = $this->getParameters($reflection); //Remove the first ($query) argument array_shift($args); - $this->setMethod($name, '\Illuminate\Database\Eloquent\Builder|\\' . $reflection->class, $args); + $this->setMethod($name, '\Illuminate\Database\Eloquent\Builder|\\' . $reflection->class, $args, $reflection->isStatic()); } } elseif (in_array($method, ['query', 'newQuery', 'newModelQuery'])) { $reflection = new \ReflectionClass($model); + $methodReflection = new \ReflectionMethod($model, $method); $builder = get_class($model->newModelQuery()); - $this->setMethod($method, "\\{$builder}|\\" . $reflection->getName()); + $this->setMethod($method, "\\{$builder}|\\" . $reflection->getName(), $methodReflection->isStatic()); } elseif (!method_exists('Illuminate\Database\Eloquent\Model', $method) && !Str::startsWith($method, 'get') ) { @@ -615,7 +618,7 @@ protected function setProperty($name, $type = null, $read = null, $write = null, } } - protected function setMethod($name, $type = '', $arguments = array()) + protected function setMethod($name, $type = '', $arguments = array(), bool $isStatic = false) { $methods = array_change_key_case($this->methods, CASE_LOWER); @@ -623,6 +626,7 @@ protected function setMethod($name, $type = '', $arguments = array()) $this->methods[$name] = array(); $this->methods[$name]['type'] = $type; $this->methods[$name]['arguments'] = $arguments; + $this->methods[$name]['isStatic'] = $isStatic; } } @@ -694,7 +698,11 @@ protected function createPhpDocs($class) continue; } $arguments = implode(', ', $method['arguments']); - $tag = Tag::createInstance("@method static {$method['type']} {$name}({$arguments})", $phpdoc); + $tag = Tag::createInstance( + "@method ".($method['isStatic'] ? '@static' : ''). + " {$method['type']} {$name}({$arguments})", + $phpdoc + ); $phpdoc->appendTag($tag); } @@ -820,12 +828,37 @@ protected function getSoftDeleteMethods($model) { $traits = class_uses(get_class($model), true); if (in_array('Illuminate\\Database\\Eloquent\\SoftDeletes', $traits)) { - $this->setMethod('forceDelete', 'bool|null', []); - $this->setMethod('restore', 'bool|null', []); + $this->setMethod( + 'forceDelete', + 'bool|null', + [], + (new \ReflectionMethod($model, 'forceDelete'))->isStatic() + ); + $this->setMethod( + 'restore', + 'bool|null', + [], + (new \ReflectionMethod($model, 'restore'))->isStatic() + ); - $this->setMethod('withTrashed', '\Illuminate\Database\Query\Builder|\\' . get_class($model), []); - $this->setMethod('withoutTrashed', '\Illuminate\Database\Query\Builder|\\' . get_class($model), []); - $this->setMethod('onlyTrashed', '\Illuminate\Database\Query\Builder|\\' . get_class($model), []); + $this->setMethod( + 'withTrashed', + '\Illuminate\Database\Query\Builder|\\' . get_class($model), + [], + (new \ReflectionMethod($model, 'withTrashed'))->isStatic() + ); + $this->setMethod( + 'withoutTrashed', + '\Illuminate\Database\Query\Builder|\\' . get_class($model), + [], + (new \ReflectionMethod($model, 'withoutTrashed'))->isStatic() + ); + $this->setMethod( + 'onlyTrashed', + '\Illuminate\Database\Query\Builder|\\' . get_class($model), + [], + (new \ReflectionMethod($model, 'onlyTrashed'))->isStatic() + ); } }