feat(ModelsCommand): add support for the new Scope attribute (#1694)

* composer fix-style

* feat(model): add support for new Scope attribute

* composer fix-style

* snapshot

* check if class_exists for psalm

* check if class_exists for psalm

* resolve psalm issue

* remove prefix slash

* revert whitespace changes

* composer fix-style

* Update composer.json

---------

Co-authored-by: laravel-ide-helper <[email protected]>
Co-authored-by: Barry vd. Heuvel <[email protected]>
This commit is contained in:
Pieter Willekens
2025-04-04 16:14:00 +02:00
committed by GitHub
co-authored by laravel-ide-helper Barry vd. Heuvel
parent d567978ebe
commit 8eec4f1ff5
5 changed files with 83 additions and 5 deletions
+2 -2
View File
@@ -29,7 +29,7 @@ $s3 = $s1 . $s2;
<?php foreach ($namespaces_by_extends_ns as $namespace => $aliases) : ?> <?php foreach ($namespaces_by_extends_ns as $namespace => $aliases) : ?>
namespace <?= $namespace === '__root' ? '' : trim($namespace, '\\') ?> { namespace <?= $namespace === '__root' ? '' : trim($namespace, '\\') ?> {
<?php foreach ($aliases as $alias) : ?> <?php foreach ($aliases as $alias) : ?>
<?php echo trim($alias->getDocComment($s1)) . "\n{$s1}" . $alias->getClassType() ?> <?= $alias->getExtendsClass() ?><?php if($alias->shouldExtendParentClass()): ?> extends <?= $alias->getParentClass() ?><?php endif; ?> { <?php echo trim($alias->getDocComment($s1)) . "\n{$s1}" . $alias->getClassType() ?> <?= $alias->getExtendsClass() ?><?php if ($alias->shouldExtendParentClass()): ?> extends <?= $alias->getParentClass() ?><?php endif; ?> {
<?php foreach ($alias->getMethods() as $method) : ?> <?php foreach ($alias->getMethods() as $method) : ?>
<?= trim($method->getDocComment($s2)) . "\n{$s2}" ?>public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>) <?= trim($method->getDocComment($s2)) . "\n{$s2}" ?>public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>)
{<?php if ($method->getDeclaringClass() !== $method->getRoot()) : ?> {<?php if ($method->getDeclaringClass() !== $method->getRoot()) : ?>
@@ -76,7 +76,7 @@ namespace <?= $namespace === '__root' ? '' : trim($namespace, '\\') ?> {
<?php endforeach; ?> <?php endforeach; ?>
<?php foreach($real_time_facades as $name): ?> <?php foreach ($real_time_facades as $name): ?>
<?php $nested = explode('\\', str_replace('\\' . class_basename($name), '', $name)); ?> <?php $nested = explode('\\', str_replace('\\' . class_basename($name), '', $name)); ?>
namespace <?php echo implode('\\', $nested); ?> { namespace <?php echo implode('\\', $nested); ?> {
/** /**
+1 -1
View File
@@ -81,7 +81,7 @@ namespace PHPSTORM_META {
<?php if (isset($expectedArgumentSets)): ?> <?php if (isset($expectedArgumentSets)): ?>
<?php foreach ($expectedArgumentSets as $name => $argumentsList) : ?> <?php foreach ($expectedArgumentSets as $name => $argumentsList) : ?>
registerArgumentsSet('<?= $name ?>', <?php foreach ($argumentsList as $i => $arg) : ?><?php if($i % 5 == 0) { registerArgumentsSet('<?= $name ?>', <?php foreach ($argumentsList as $i => $arg) : ?><?php if ($i % 5 == 0) {
echo "\n"; echo "\n";
} ?><?= var_export($arg, true); ?>,<?php endforeach; ?>); } ?><?= var_export($arg, true); ?>,<?php endforeach; ?>);
<?php endforeach; ?> <?php endforeach; ?>
+4 -2
View File
@@ -669,9 +669,11 @@ class ModelsCommand extends Command
$comment = $this->getCommentFromDocBlock($reflection); $comment = $this->getCommentFromDocBlock($reflection);
$this->setProperty($name, null, null, true, $comment); $this->setProperty($name, null, null, true, $comment);
} }
} elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery' && $method !== 'scope' && $method !== 'scopes') { } elseif (!empty($reflection->getAttributes('Illuminate\Database\Eloquent\Attributes\Scope')) || (Str::startsWith($method, 'scope') && $method !== 'scopeQuery' && $method !== 'scope' && $method !== 'scopes')) {
$scopeUsingAttribute = !empty($reflection->getAttributes('Illuminate\Database\Eloquent\Attributes\Scope'));
//Magic scope<name>Attribute //Magic scope<name>Attribute
$name = Str::camel(substr($method, 5)); $name = $scopeUsingAttribute ? $method : Str::camel(substr($method, 5));
if (!empty($name)) { if (!empty($name)) {
$comment = $this->getCommentFromDocBlock($reflection); $comment = $this->getCommentFromDocBlock($reflection);
$args = $this->getParameters($reflection); $args = $this->getParameters($reflection);
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\QueryScopes\Models;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
/**
* @comment Scope using the 'Scope' attribute
* @param Builder $query
* @return void
*/
#[Scope]
protected function local(Builder $query): void
{
$query->where('ip_address', '127.0.0.1');
}
/**
* @comment Scope using the 'scope' prefix
* @param Builder $query
* @return void
*/
protected function scopeSystem(Builder $query): void
{
$query->where('system', true);
}
}
@@ -4,6 +4,49 @@ declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\QueryScopes\Models; namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\QueryScopes\Models;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
*
*
* @method static Builder<static>|Comment local() Scope using the 'Scope' attribute
* @method static Builder<static>|Comment newModelQuery()
* @method static Builder<static>|Comment newQuery()
* @method static Builder<static>|Comment query()
* @method static Builder<static>|Comment system() Scope using the 'scope' prefix
* @mixin \Eloquent
*/
class Comment extends Model
{
/**
* @comment Scope using the 'Scope' attribute
* @param Builder $query
* @return void
*/
#[Scope]
protected function local(Builder $query): void
{
$query->where('ip_address', '127.0.0.1');
}
/**
* @comment Scope using the 'scope' prefix
* @param Builder $query
* @return void
*/
protected function scopeSystem(Builder $query): void
{
$query->where('system', true);
}
}
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\QueryScopes\Models;
/** /**
* *
* *