Skip to content

Commit 1dd3c30

Browse files
committed
feat(model): add support for new Scope attribute
1 parent aee6780 commit 1dd3c30

File tree

4 files changed

+83
-8
lines changed

4 files changed

+83
-8
lines changed

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
"ext-json": "*",
2626
"barryvdh/reflection-docblock": "^2.3",
2727
"composer/class-map-generator": "^1.0",
28-
"illuminate/console": "^11.15 || ^12",
29-
"illuminate/database": "^11.15 || ^12",
30-
"illuminate/filesystem": "^11.15 || ^12",
31-
"illuminate/support": "^11.15 || ^12"
28+
"illuminate/console": "^11.15 || ^12.4.1",
29+
"illuminate/database": "^11.15 || ^12.4.1",
30+
"illuminate/filesystem": "^11.15 || ^12.4.1",
31+
"illuminate/support": "^11.15 || ^12.4.1"
3232
},
3333
"require-dev": {
3434
"ext-pdo_sqlite": "*",
3535
"friendsofphp/php-cs-fixer": "^3",
36-
"illuminate/config": "^11.15 || ^12",
37-
"illuminate/view": "^11.15 || ^12",
36+
"illuminate/config": "^11.15 || ^12.4.1",
37+
"illuminate/view": "^11.15 || ^12.4.1",
3838
"mockery/mockery": "^1.4",
3939
"orchestra/testbench": "^9.2 || ^10",
4040
"phpunit/phpunit": "^10.5 || ^11.5.3",

src/Console/ModelsCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Illuminate\Contracts\Database\Eloquent\Castable;
2626
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
2727
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
28+
use Illuminate\Database\Eloquent\Attributes\Scope;
2829
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
2930
use Illuminate\Database\Eloquent\Casts\AsCollection;
3031
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
@@ -669,9 +670,11 @@ public function getPropertiesFromMethods($model)
669670
$comment = $this->getCommentFromDocBlock($reflection);
670671
$this->setProperty($name, null, null, true, $comment);
671672
}
672-
} elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery' && $method !== 'scope' && $method !== 'scopes') {
673+
} elseif (!empty($reflection->getAttributes(Scope::class)) || (Str::startsWith($method, 'scope') && $method !== 'scopeQuery' && $method !== 'scope' && $method !== 'scopes')) {
674+
$scopeUsingAttribute = !empty($reflection->getAttributes(Scope::class));
675+
673676
//Magic scope<name>Attribute
674-
$name = Str::camel(substr($method, 5));
677+
$name = $scopeUsingAttribute ? $method : Str::camel(substr($method, 5));
675678
if (!empty($name)) {
676679
$comment = $this->getCommentFromDocBlock($reflection);
677680
$args = $this->getParameters($reflection);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
$query->where('ip_address', '127.0.0.1');
21+
}
22+
23+
/**
24+
* @comment Scope using the 'scope' prefix
25+
* @param Builder $query
26+
* @return void
27+
*/
28+
protected function scopeSystem(Builder $query): void {
29+
$query->where('system', true);
30+
}
31+
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,47 @@
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+
$query->where('ip_address', '127.0.0.1');
31+
}
32+
33+
/**
34+
* @comment Scope using the 'scope' prefix
35+
* @param Builder $query
36+
* @return void
37+
*/
38+
protected function scopeSystem(Builder $query): void {
39+
$query->where('system', true);
40+
}
41+
}
42+
<?php
43+
44+
declare(strict_types=1);
45+
46+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\QueryScopes\Models;
47+
748
/**
849
*
950
*

0 commit comments

Comments
 (0)