Stillat / vscode-antlers-language-server

Provides rich language features for Statamic's Antlers templating language, including code completions, syntax highlighting, and more.
https://antlers.dev
MIT License
38 stars 3 forks source link

Unexpected type supplied to modifier <modifier>. Expected void got string #1

Closed jelleroorda closed 3 years ago

jelleroorda commented 3 years ago

Even though nothing is being supplied to the modifier? See the error here:

afbeelding

Modifier:

<?php

namespace App\Modifiers;

use Statamic\Facades\Config;
use Statamic\Modifiers\Modifier;

class SanitizeFixed extends Modifier
{
    /**
     * Modify a value.
     *
     * @param mixed  $value    The value to be modified
     * @return mixed
     */
    public function index($value)
    {
        // We double encode characters, so we don't get any breaking json when we try to feed
        // json containing quotes inside our vue components.
        return htmlspecialchars($value, ENT_QUOTES, Config::get('statamic.system.charset', 'UTF-8'), true);
    }
}

Thanks for the awesome extension!

JohnathonKoster commented 3 years ago

Thanks for the report!

I think I will adjust the default behavior of custom/unknown items so that the behavior is less obnoxious by default. It's pulling the return type of modifier to it's left (string), and comparing it to the known list of input types for the current modifier sanitize_fixed. If it doesn't have any information on those types, it assumes void at the moment.

If you see a .antlers.json file at the root of your project, that means the PHP analyzer is working on your project. If so, you can use IDE hints in your PHP comment's to let the Antlers language server know about your custom input/return types:

https://antlers.dev/docs/php-tag-ide-hints#specifying-parameter-input-variable-types

For modifiers, these would go on the modifier's class docblock instead of the method. The language server looks for different docblock properties to not conflict with any future behavior of Statamic (@input identifies input to tags and modifiers, and @returns specifies the possible return types):

<?php

namespace App\Modifiers;

use Statamic\Facades\Config;
use Statamic\Modifiers\Modifier;

/**
 * @input string value The value to sanitize.
 * @returns string
 */
class SanitizeFixed extends Modifier
{
}