Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop calling a method static if its not static #872

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .editorconfig

This file was deleted.

55 changes: 44 additions & 11 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Expand Down Expand Up @@ -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')
) {
Expand Down Expand Up @@ -615,14 +618,15 @@ 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);

if (!isset($methods[strtolower($name)])) {
$this->methods[$name] = array();
$this->methods[$name]['type'] = $type;
$this->methods[$name]['arguments'] = $arguments;
$this->methods[$name]['isStatic'] = $isStatic;
}
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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()
);
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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);
Expand Down
43 changes: 42 additions & 1 deletion tests/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand All @@ -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;
}
}