55namespace Korridor \LaravelComputedAttributes \Console ;
66
77use Illuminate \Console \Command ;
8- use Illuminate \Database \Eloquent \Model ;
8+ use Illuminate \Database \Eloquent \Builder ;
99use Illuminate \Support \Collection ;
10- use Korridor \LaravelComputedAttributes \ComputedAttributes ;
10+ use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
11+ use Korridor \LaravelComputedAttributes \ComputedAttributesInterface ;
1112use Korridor \LaravelComputedAttributes \Parser \ModelAttributeParser ;
1213use Korridor \LaravelComputedAttributes \Parser \ModelAttributesEntry ;
1314use Korridor \LaravelComputedAttributes \Parser \ParsingException ;
@@ -46,13 +47,19 @@ class GenerateComputedAttributes extends Command
4647 */
4748 public function handle (): int
4849 {
50+ $ this ->info ('Parsing arguments... ' );
51+
52+ // Validate modelsAttributes argument
4953 $ modelsWithAttributes = $ this ->argument ('modelsAttributes ' );
54+ if ($ modelsWithAttributes !== null && !is_string ($ modelsWithAttributes )) {
55+ $ this ->error ('Argument modelsAttributes needs to be a string ' );
5056
51- $ this ->info ('Parsing arguments... ' );
57+ return self ::FAILURE ;
58+ }
5259
5360 // Validate chunkSize option
5461 $ chunkSizeRaw = $ this ->option ('chunkSize ' );
55- if (preg_match ('/^\d+$/ ' , $ chunkSizeRaw )) {
62+ if (is_string ( $ chunkSizeRaw ) && preg_match ('/^\d+$/ ' , $ chunkSizeRaw )) {
5663 $ chunkSize = (int ) $ chunkSizeRaw ;
5764 if ($ chunkSize < 1 ) {
5865 $ this ->error ('Option chunkSize needs to be greater than zero ' );
@@ -68,7 +75,7 @@ public function handle(): int
6875 // Validate block option
6976 $ chunkRaw = $ this ->option ('chunk ' );
7077 if ($ chunkRaw !== null ) {
71- if (preg_match ('/^\d+$/ ' , $ chunkRaw )) {
78+ if (is_string ( $ chunkRaw ) && preg_match ('/^\d+$/ ' , $ chunkRaw )) {
7279 $ chunk = (int ) $ chunkRaw ;
7380 if ($ chunk < 0 ) {
7481 $ this ->error ('Option chunk needs to be greater or equal than zero ' );
@@ -98,7 +105,7 @@ public function handle(): int
98105 // Calculate
99106 foreach ($ modelAttributesEntries as $ modelAttributesEntry ) {
100107 $ model = $ modelAttributesEntry ->getModel ();
101- /** @var Model|ComputedAttributes $modelInstance */
108+ /** @var ComputedAttributesInterface $modelInstance */
102109 $ modelInstance = new $ model ();
103110 $ attributes = $ modelAttributesEntry ->getAttributes ();
104111
@@ -123,9 +130,14 @@ public function handle(): int
123130 return self ::SUCCESS ;
124131 }
125132
126- private function generateModels (Collection $ models , array $ attributes , ModelAttributesEntry $ modelAttributesEntry ): void
133+ /**
134+ * @param EloquentCollection<array-key, ComputedAttributesInterface> $models
135+ * @param array<string> $attributes
136+ * @param ModelAttributesEntry $modelAttributesEntry
137+ * @return void
138+ */
139+ private function generateModels (EloquentCollection $ models , array $ attributes , ModelAttributesEntry $ modelAttributesEntry ): void
127140 {
128- /* @var Model|ComputedAttributes $modelResult */
129141 foreach ($ models as $ modelResult ) {
130142 foreach ($ attributes as $ attribute ) {
131143 $ modelResult ->setComputedAttributeValue ($ attribute );
0 commit comments