Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions php-templates/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,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'])
]))
Expand All @@ -175,6 +178,20 @@ protected function getInfo($className)
];
}

/**
* @return array<int, string>
*/
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();
}

protected function getScopeParameterInfo(\ReflectionParameter $parameter): array
{
$result = [
Expand Down
18 changes: 10 additions & 8 deletions src/features/auth.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -126,22 +127,23 @@ 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"
? (classArg.className ?? 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 {
Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ declare namespace Eloquent {
observers: Observer[];
scopes: Scope[];
extends: string | null;
name_cases: string[];
}

interface Attribute {
Expand All @@ -97,6 +98,7 @@ declare namespace Eloquent {
appended: null;
cast: string | null;
title_case: string;
name_cases: string[];
documented: boolean;
}

Expand Down
8 changes: 8 additions & 0 deletions src/repositories/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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<Eloquent.Models>({
load,
pattern: modelPaths
Expand Down
17 changes: 17 additions & 0 deletions src/templates/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,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'])
]))
Expand All @@ -175,6 +178,20 @@ $models = new class($factory) {
];
}

/**
* @return array<int, string>
*/
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();
}

protected function getScopeParameterInfo(\\ReflectionParameter $parameter): array
{
$result = [
Expand Down