Skip to content

Commit 50cbc62

Browse files
Merge pull request #320 from laravel/model-doc-blocks
Option to disable model doc block generation + fix doc block extends
2 parents 0f462ab + bdc9b09 commit 50cbc62

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@
113113
"default": true,
114114
"description": "Show popups for errors."
115115
},
116+
"Laravel.eloquent.generateDocBlocks": {
117+
"type": "boolean",
118+
"default": true,
119+
"description": "Automatically generate Eloquent doc blocks for models as IDE helpers."
120+
},
116121
"Laravel.blade.autoSpaceTags": {
117122
"type": "boolean",
118123
"default": true,

php-templates/models.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,30 @@ protected function collectExistingProperties($reflection)
119119
return collect();
120120
}
121121

122+
protected function getParentClass(\ReflectionClass $reflection)
123+
{
124+
if (!$reflection->getParentClass()) {
125+
return null;
126+
}
127+
128+
$parent = $reflection->getParentClass()->getName();
129+
130+
if ($parent === \Illuminate\Database\Eloquent\Model::class) {
131+
return null;
132+
}
133+
134+
return \Illuminate\Support\Str::start($parent, '\\');
135+
}
136+
122137
protected function getInfo($className)
123138
{
124139
if (($data = $this->fromArtisan($className)) === null) {
125140
return null;
126141
}
127142

128-
$reflection = (new \ReflectionClass($className));
143+
$reflection = new \ReflectionClass($className);
144+
145+
$data["extends"] = $this->getParentClass($reflection);
129146

130147
$existingProperties = $this->collectExistingProperties($reflection);
131148

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ declare namespace Eloquent {
8282
events: Event[];
8383
observers: Observer[];
8484
scopes: string[];
85+
extends: string | null;
8586
}
8687

8788
interface Attribute {

src/support/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ type ConfigKey =
1010
| "tests.ssh.enabled"
1111
| "tests.suiteSuffix"
1212
| "showErrorPopups"
13-
| "blade.autoSpaceTags";
13+
| "blade.autoSpaceTags"
14+
| "eloquent.generateDocBlocks";
1415

1516
export const config = <T>(key: ConfigKey, fallback: T): T =>
1617
vscode.workspace.getConfiguration("Laravel").get<T>(key, fallback);

src/support/docblocks.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import fs from "fs";
22
import { Eloquent } from "..";
3+
import { config } from "./config";
34
import { internalVendorPath } from "./project";
45
import { indent } from "./util";
56

67
interface ClassBlock {
78
namespace: string;
89
className: string;
910
blocks: string[];
11+
extends: string | null;
1012
}
1113

1214
const modelBuilderType = (className: string) =>
@@ -16,7 +18,7 @@ export const writeEloquentDocBlocks = (
1618
models: Eloquent.Models,
1719
builderMethods: Eloquent.BuilderMethod[],
1820
) => {
19-
if (!models) {
21+
if (!models || !config("eloquent.generateDocBlocks", true)) {
2022
return;
2123
}
2224

@@ -28,6 +30,7 @@ export const writeEloquentDocBlocks = (
2830
namespace: pathParts.join("\\"),
2931
className: cls || "",
3032
blocks: getBlocks(model, cls || "", builderMethods),
33+
extends: model.extends || null,
3134
};
3235
});
3336

@@ -113,7 +116,9 @@ const getBlocks = (
113116
.concat(
114117
[...model.scopes, "newModelQuery", "newQuery", "query"].map(
115118
(method) => {
116-
return `@method static ${modelBuilderType(className)} ${method}()`;
119+
return `@method static ${modelBuilderType(
120+
className,
121+
)} ${method}()`;
117122
},
118123
),
119124
)
@@ -178,7 +183,9 @@ const classToDocBlock = (block: ClassBlock, namespace: string) => {
178183
...block.blocks,
179184
" * @mixin \\Illuminate\\Database\\Query\\Builder",
180185
" */",
181-
`class ${block.className} extends \\Illuminate\\Database\\Eloquent\\Model`,
186+
`class ${block.className} extends ${
187+
block.extends ?? "\\Illuminate\\Database\\Eloquent\\Model"
188+
}`,
182189
`{`,
183190
indent("//"),
184191
`}`,
@@ -205,7 +212,9 @@ const getAttributeBlocks = (
205212

206213
if (!["accessor", "attribute"].includes(attr.cast || "")) {
207214
blocks.push(
208-
`@method static ${modelBuilderType(className)} where${attr.title_case}($value)`,
215+
`@method static ${modelBuilderType(className)} where${
216+
attr.title_case
217+
}($value)`,
209218
);
210219
}
211220

src/templates/models.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,30 @@ $models = new class($factory) {
119119
return collect();
120120
}
121121
122+
protected function getParentClass(\\ReflectionClass $reflection)
123+
{
124+
if (!$reflection->getParentClass()) {
125+
return null;
126+
}
127+
128+
$parent = $reflection->getParentClass()->getName();
129+
130+
if ($parent === \\Illuminate\\Database\\Eloquent\\Model::class) {
131+
return null;
132+
}
133+
134+
return \\Illuminate\\Support\\Str::start($parent, '\\\\');
135+
}
136+
122137
protected function getInfo($className)
123138
{
124139
if (($data = $this->fromArtisan($className)) === null) {
125140
return null;
126141
}
127142
128-
$reflection = (new \\ReflectionClass($className));
143+
$reflection = new \\ReflectionClass($className);
144+
145+
$data["extends"] = $this->getParentClass($reflection);
129146
130147
$existingProperties = $this->collectExistingProperties($reflection);
131148

0 commit comments

Comments
 (0)