-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved attribute handling with types and Attribute support (#1567)
* Add `AllowDynamicProperties` Attribute to cooperate with php8.2 deprecation * lint snapshot * Fix collection attribute typing * Update CHANGELOG.md * Update CHANGELOG.md * Update CHANGELOG.md * Update composer.json * Fix collectino with template types WIP * Add `AllowDynamicProperties` Attribute to cooperate with php8.2 deprecation * Fix collection attribute typing * Update CHANGELOG.md * Update CHANGELOG.md * Update composer.json * composer fix-style * Fix tests * Fix tests * composer fix-style * Fix collectino with complex template types{ * Delete phpactor * Install from scrumble repo * Implemented correct attribute handling * composer fix-style * Revert "Install from scrumble repo" This reverts commit 3f1057a. * Re-add barryvdh links * Revert "Update composer.json" This reverts commit 98252fe. * Fix pivot test * Correct CHANGELOG.md with attribute merge request * More clear PhpDocTypeParser filename and clarified directory according to PSR12 --------- Co-authored-by: GeoSot <[email protected]> Co-authored-by: laravel-ide-helper <[email protected]> Co-authored-by: Luukdewaaier <[email protected]> Co-authored-by: Luuk de Weijer <[email protected]>
- Loading branch information
1 parent
6338abb
commit c6509b6
Showing
15 changed files
with
463 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Barryvdh\LaravelIdeHelper\Parsers; | ||
|
||
class PhpDocReturnTypeParser | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private string $typeAlias; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private array $namespaceAliases; | ||
|
||
/** | ||
* @param string $typeAlias | ||
* @param array $namespaceAliases | ||
*/ | ||
public function __construct(string $typeAlias, array $namespaceAliases) | ||
{ | ||
$this->typeAlias = $typeAlias; | ||
$this->namespaceAliases = $namespaceAliases; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function parse(): string|null | ||
{ | ||
$matches = []; | ||
preg_match('/(\w+)(<.*>)/', $this->typeAlias, $matches); | ||
$matchCount = count($matches); | ||
|
||
if ($matchCount === 0 || $matchCount === 1) { | ||
return null; | ||
} | ||
|
||
if (empty($this->namespaceAliases[$matches[1]])) { | ||
return null; | ||
} | ||
|
||
return $this->namespaceAliases[$matches[1]] . $this->parseTemplate($matches[2] ?? null); | ||
} | ||
|
||
/** | ||
* @param string|null $template | ||
* @return string | ||
*/ | ||
private function parseTemplate(string|null $template): string | ||
{ | ||
if ($template === null || $template === '') { | ||
return ''; | ||
} | ||
|
||
$type = ''; | ||
$result = ''; | ||
|
||
foreach (str_split($template) as $char) { | ||
$match = preg_match('/[A-z]/', $char); | ||
|
||
if (!$match) { | ||
$type = $this->namespaceAliases[$type] ?? $type; | ||
$result .= $type; | ||
$result .= $char; | ||
$type = ''; | ||
|
||
continue; | ||
} | ||
|
||
$type .= $char; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
tests/Console/ModelsCommand/Attributes/Models/BackedAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Attributes\Models; | ||
|
||
use Illuminate\Database\Eloquent\Casts\Attribute; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class BackedAttribute extends Model | ||
{ | ||
protected function name(): Attribute | ||
{ | ||
return new Attribute( | ||
function (?string $name): ?string { | ||
return $name; | ||
}, | ||
function (?string $name): ?string { | ||
return $name; | ||
} | ||
); | ||
} | ||
|
||
protected function nameRead(): Attribute | ||
{ | ||
return new Attribute( | ||
function (?string $name): ?string { | ||
return $name; | ||
}, | ||
); | ||
} | ||
|
||
protected function nameWrite(): Attribute | ||
{ | ||
return new Attribute( | ||
set: function (?string $name): ?string { | ||
return $name; | ||
}, | ||
); | ||
} | ||
|
||
protected function nonBackedSet(): Attribute | ||
{ | ||
return new Attribute( | ||
set: function (?string $name): ?string { | ||
return $name; | ||
}, | ||
); | ||
} | ||
|
||
protected function nonBackedGet(): Attribute | ||
{ | ||
return new Attribute( | ||
get: function (): ?string { | ||
return 'test'; | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.