Skip to content

Support for links for policies with variable as 2nd argument #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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 @@ -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'])
]))
Expand All @@ -166,6 +169,20 @@ protected function getInfo($className)
$className => $data,
];
}

/**
* @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();
}
};

$builder = new class($docblocks) {
Expand Down
16 changes: 8 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,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 {
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: string[];
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 @@ -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<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 @@ -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'])
]))
Expand All @@ -166,6 +169,20 @@ $models = new class($factory) {
$className => $data,
];
}

/**
* @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();
}
};

$builder = new class($docblocks) {
Expand Down