Skip to content

Commit 8eec4f1

Browse files
pataarlaravel-ide-helperbarryvdh
authored
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 <laravel-ide-helper@users.noreply.github.com> Co-authored-by: Barry vd. Heuvel <barryvdh@gmail.com>
1 parent d567978 commit 8eec4f1

File tree

5 files changed

+83
-5
lines changed

5 files changed

+83
-5
lines changed

resources/views/helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<?php foreach ($namespaces_by_extends_ns as $namespace => $aliases) : ?>
3030
namespace <?= $namespace === '__root' ? '' : trim($namespace, '\\') ?> {
3131
<?php foreach ($aliases as $alias) : ?>
32-
<?php echo trim($alias->getDocComment($s1)) . "\n{$s1}" . $alias->getClassType() ?> <?= $alias->getExtendsClass() ?><?php if($alias->shouldExtendParentClass()): ?> extends <?= $alias->getParentClass() ?><?php endif; ?> {
32+
<?php echo trim($alias->getDocComment($s1)) . "\n{$s1}" . $alias->getClassType() ?> <?= $alias->getExtendsClass() ?><?php if ($alias->shouldExtendParentClass()): ?> extends <?= $alias->getParentClass() ?><?php endif; ?> {
3333
<?php foreach ($alias->getMethods() as $method) : ?>
3434
<?= trim($method->getDocComment($s2)) . "\n{$s2}" ?>public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>)
3535
{<?php if ($method->getDeclaringClass() !== $method->getRoot()) : ?>
@@ -76,7 +76,7 @@
7676

7777
<?php endforeach; ?>
7878

79-
<?php foreach($real_time_facades as $name): ?>
79+
<?php foreach ($real_time_facades as $name): ?>
8080
<?php $nested = explode('\\', str_replace('\\' . class_basename($name), '', $name)); ?>
8181
namespace <?php echo implode('\\', $nested); ?> {
8282
/**

resources/views/meta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181

8282
<?php if (isset($expectedArgumentSets)): ?>
8383
<?php foreach ($expectedArgumentSets as $name => $argumentsList) : ?>
84-
registerArgumentsSet('<?= $name ?>', <?php foreach ($argumentsList as $i => $arg) : ?><?php if($i % 5 == 0) {
84+
registerArgumentsSet('<?= $name ?>', <?php foreach ($argumentsList as $i => $arg) : ?><?php if ($i % 5 == 0) {
8585
echo "\n";
8686
} ?><?= var_export($arg, true); ?>,<?php endforeach; ?>);
8787
<?php endforeach; ?>

src/Console/ModelsCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,11 @@ public function getPropertiesFromMethods($model)
669669
$comment = $this->getCommentFromDocBlock($reflection);
670670
$this->setProperty($name, null, null, true, $comment);
671671
}
672-
} elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery' && $method !== 'scope' && $method !== 'scopes') {
672+
} elseif (!empty($reflection->getAttributes('Illuminate\Database\Eloquent\Attributes\Scope')) || (Str::startsWith($method, 'scope') && $method !== 'scopeQuery' && $method !== 'scope' && $method !== 'scopes')) {
673+
$scopeUsingAttribute = !empty($reflection->getAttributes('Illuminate\Database\Eloquent\Attributes\Scope'));
674+
673675
//Magic scope<name>Attribute
674-
$name = Str::camel(substr($method, 5));
676+
$name = $scopeUsingAttribute ? $method : Str::camel(substr($method, 5));
675677
if (!empty($name)) {
676678
$comment = $this->getCommentFromDocBlock($reflection);
677679
$args = $this->getParameters($reflection);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\QueryScopes\Models;
6+
7+
use Illuminate\Database\Eloquent\Attributes\Scope;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
class Comment extends Model
12+
{
13+
/**
14+
* @comment Scope using the 'Scope' attribute
15+
* @param Builder $query
16+
* @return void
17+
*/
18+
#[Scope]
19+
protected function local(Builder $query): void
20+
{
21+
$query->where('ip_address', '127.0.0.1');
22+
}
23+
24+
/**
25+
* @comment Scope using the 'scope' prefix
26+
* @param Builder $query
27+
* @return void
28+
*/
29+
protected function scopeSystem(Builder $query): void
30+
{
31+
$query->where('system', true);
32+
}
33+
}

tests/Console/ModelsCommand/QueryScopes/__snapshots__/Test__test__1.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,49 @@
44

55
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\QueryScopes\Models;
66

7+
use Illuminate\Database\Eloquent\Attributes\Scope;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
/**
12+
*
13+
*
14+
* @method static Builder<static>|Comment local() Scope using the 'Scope' attribute
15+
* @method static Builder<static>|Comment newModelQuery()
16+
* @method static Builder<static>|Comment newQuery()
17+
* @method static Builder<static>|Comment query()
18+
* @method static Builder<static>|Comment system() Scope using the 'scope' prefix
19+
* @mixin \Eloquent
20+
*/
21+
class Comment extends Model
22+
{
23+
/**
24+
* @comment Scope using the 'Scope' attribute
25+
* @param Builder $query
26+
* @return void
27+
*/
28+
#[Scope]
29+
protected function local(Builder $query): void
30+
{
31+
$query->where('ip_address', '127.0.0.1');
32+
}
33+
34+
/**
35+
* @comment Scope using the 'scope' prefix
36+
* @param Builder $query
37+
* @return void
38+
*/
39+
protected function scopeSystem(Builder $query): void
40+
{
41+
$query->where('system', true);
42+
}
43+
}
44+
<?php
45+
46+
declare(strict_types=1);
47+
48+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\QueryScopes\Models;
49+
750
/**
851
*
952
*

0 commit comments

Comments
 (0)