diff --git a/php-templates/models.php b/php-templates/models.php index 3946d402..fb61b82f 100644 --- a/php-templates/models.php +++ b/php-templates/models.php @@ -144,11 +144,14 @@ protected function getInfo($className) $data["extends"] = $this->getParentClass($reflection); + $data['name_cases'] = $this->getNameCases(str($className)->afterLast('\\')->toString()); + $existingProperties = $this->collectExistingProperties($reflection); $data['attributes'] = collect($data['attributes']) ->map(fn($attrs) => array_merge($attrs, [ 'title_case' => str($attrs['name'])->title()->replace('_', '')->toString(), + 'name_cases' => $this->getNameCases($attrs['name']), 'documented' => $existingProperties->contains($attrs['name']), 'cast' => $this->getCastReturnType($attrs['cast']) ])) @@ -166,6 +169,20 @@ protected function getInfo($className) $className => $data, ]; } + + /** + * @return array + */ + protected function getNameCases(string $name): array + { + return collect([ + $name, + str($name)->camel()->toString(), + str($name)->snake()->toString(), + str($name)->studly()->toString(), + str($name)->studly()->lower()->toString(), + ])->unique()->values()->toArray(); + } }; $builder = new class($docblocks) { diff --git a/src/features/auth.ts b/src/features/auth.ts index cb8d127c..ec63c14d 100644 --- a/src/features/auth.ts +++ b/src/features/auth.ts @@ -1,6 +1,7 @@ import { notFound } from "@src/diagnostic"; import AutocompleteResult from "@src/parser/AutocompleteResult"; import { AuthItem, getPolicies } from "@src/repositories/auth"; +import { getModelByName } from "@src/repositories/models"; import { config } from "@src/support/config"; import { findHoverMatchesInDoc } from "@src/support/doc"; import { detectedRange, detectInDoc } from "@src/support/parser"; @@ -126,22 +127,21 @@ const analyzeParam = ( // @ts-ignore const nextArg = item.arguments.children[1].children[0]; - let classArg: string | null = null; - if (nextArg?.type === "array") { - classArg = nextArg.children[0]?.value?.className; - } else { - classArg = nextArg?.className; - } + const classArg = nextArg?.type === "array" ? + nextArg.children[0]?.value : nextArg; + + const modelClass = classArg?.type === "variable" ? + getModelByName(classArg.name)?.class : classArg?.className; - if (!classArg) { + if (!modelClass) { // If it's not a class we can even identify, just ignore it return { missingReason: "ignored", }; } - const found = policies.find((items) => items.model === classArg); + const found = policies.find((items) => items.model === modelClass); if (!found) { return { diff --git a/src/index.d.ts b/src/index.d.ts index dbc537e8..0b7650de 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -83,6 +83,7 @@ declare namespace Eloquent { observers: Observer[]; scopes: string[]; extends: string | null; + name_cases: string[]; } interface Attribute { @@ -97,6 +98,7 @@ declare namespace Eloquent { appended: null; cast: string | null; title_case: string; + name_cases: string[]; documented: boolean; } diff --git a/src/repositories/models.ts b/src/repositories/models.ts index 3d045a3c..411618ce 100644 --- a/src/repositories/models.ts +++ b/src/repositories/models.ts @@ -22,6 +22,14 @@ const load = () => { }); }; +export const getModelByName = (name: string): Eloquent.Model | undefined => { + const model = Object.entries(getModels().items).find(([, value]) => { + return value.name_cases.includes(name); + }); + + return model?.[1]; +}; + export const getModels = repository({ load, pattern: modelPaths diff --git a/src/templates/models.ts b/src/templates/models.ts index 158b4952..61ff600a 100644 --- a/src/templates/models.ts +++ b/src/templates/models.ts @@ -144,11 +144,14 @@ $models = new class($factory) { $data["extends"] = $this->getParentClass($reflection); + $data['name_cases'] = $this->getNameCases(str($className)->afterLast('\\\\')->toString()); + $existingProperties = $this->collectExistingProperties($reflection); $data['attributes'] = collect($data['attributes']) ->map(fn($attrs) => array_merge($attrs, [ 'title_case' => str($attrs['name'])->title()->replace('_', '')->toString(), + 'name_cases' => $this->getNameCases($attrs['name']), 'documented' => $existingProperties->contains($attrs['name']), 'cast' => $this->getCastReturnType($attrs['cast']) ])) @@ -166,6 +169,20 @@ $models = new class($factory) { $className => $data, ]; } + + /** + * @return array + */ + protected function getNameCases(string $name): array + { + return collect([ + $name, + str($name)->camel()->toString(), + str($name)->snake()->toString(), + str($name)->studly()->toString(), + str($name)->studly()->lower()->toString(), + ])->unique()->values()->toArray(); + } }; $builder = new class($docblocks) {