Skip to content
New issue

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

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

Already on GitHub? # to your account

ModelsCommand->checkForCastableCasts does not support AsArrayObject #1305

Closed
CrixuAMG opened this issue Jan 23, 2022 · 9 comments · Fixed by #1306
Closed

ModelsCommand->checkForCastableCasts does not support AsArrayObject #1305

CrixuAMG opened this issue Jan 23, 2022 · 9 comments · Fixed by #1306
Labels

Comments

@CrixuAMG
Copy link

Versions:

  • ide-helper Version: 2.12.0
  • Laravel Version: 8.80.0
  • PHP Version: 7.4

Description and steps to reproduce:

When setting the cast of a JSON object to \Illuminate\Database\Eloquent\Casts\AsArrayObject, ide-helper:models fails to get the return type of that cast. At line 1293 of the ModelsCommand class both methods return null, even though the AsArrayObject class contains the following @return tag in the docblock.

    /*
     * @return object|string
     */

The error:

> php artisan ide-helper:models --nowrite
Exception: Return value of Barryvdh\LaravelIdeHelper\Console\ModelsCommand::checkForCastableCasts() must be of the type string, null returned
@CrixuAMG CrixuAMG added the bug label Jan 23, 2022
@mfn
Copy link
Collaborator

mfn commented Jan 24, 2022

@binotaliu is this related to #1287 ? 🤔

@CrixuAMG
Copy link
Author

Did a little bit more looking into how the return type is detected, it seems logical that the command is failing since the get function in the AsArrayObject class has no doc block and no return type. A possible fix could be setting a default mixed value for when no return value could be detected, another would be opening a pull request to illuminate/database containing doc blocks/return types, what would the preferred option be? Or is there a better/easier fix?

Here's the full class for reference:

<?php

namespace Illuminate\Database\Eloquent\Casts;

use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class AsArrayObject implements Castable
{
    /**
     * Get the caster class to use when casting from / to this cast target.
     *
     * @param  array  $arguments
     * @return object|string
     */
    public static function castUsing(array $arguments)
    {
        return new class implements CastsAttributes
        {
            public function get($model, $key, $value, $attributes)
            {
                return isset($attributes[$key]) ? new ArrayObject(json_decode($attributes[$key], true)) : null;
            }

            public function set($model, $key, $value, $attributes)
            {
                return [$key => json_encode($value)];
            }

            public function serialize($model, string $key, $value, array $attributes)
            {
                return $value->getArrayCopy();
            }
        };
    }
}

@binotaliu
Copy link
Contributor

@mfn yes. return type should be ?string. I'll open a new PR to fix this soon.

@vadimwe
Copy link

vadimwe commented Jan 24, 2022

Same problem:

Exception: Barryvdh\LaravelIdeHelper\Console\ModelsCommand::checkForCastableCasts(): Return value must be of type string, null returned Could not analyze class App\Models\Projects\Project.

Cast:
public function get($model, string $key, $value, array $attributes) { return collect(json_decode($value, true))->map(function ($serviceItem) { return new InvoiceServiceItem($serviceItem['title'], $serviceItem['qty'], $serviceItem['itemPrice']); }); }

public function set($model, string $key, $value, array $attributes) { return json_encode($value); }

@mfn
Copy link
Collaborator

mfn commented Jan 24, 2022

@vadimwe doe the change in #1306 work for you?

@mfn
Copy link
Collaborator

mfn commented Jan 24, 2022

Bug fix 🤞🏼 has just been released as https://github.com/barryvdh/laravel-ide-helper/releases/tag/v2.12.1

@hotmeteor
Copy link

Wow, came here looking for this. Fixed 28 minutes ago 😂 Thanks all!

@CrixuAMG
Copy link
Author

Thanks for the quick fix! 🙏

@vadimwe
Copy link

vadimwe commented Jan 26, 2022

@vadimwe doe the change in #1306 work for you?

Yes! thank you!

d3v2a pushed a commit to d3v2a/laravel-ide-helper that referenced this issue Feb 16, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants