admin管理员组

文章数量:1312658

I'm trying to write a rector set for my project to migrate from laravel/collective to spatie/html currently my refactored code looks like this

- <?php echo \Collective\Html\FormFacade::button('<i class="fa fa-fw fa-close" aria-hidden="true"></i> ' . trans('LaravelLogger::laravel-logger.modals.shared.btnCancel'), array('class' => 'btn btn-outline pull-left btn-flat', 'type' => 'button', 'data-dismiss' => 'modal' )) ?>
- <?php echo \Collective\Html\FormFacade::button('<i class="fa ' . $actionBtnIcon . '" aria-hidden="true"></i> ' . $btnSubmitText, array('class' => 'btn btn-' . $modalClass . ' pull-right btn-flat', 'type' => 'button', 'id' => 'confirm' )) ?>
+ <?php echo html()->button('<i class="fa fa-fw fa-close" aria-hidden="true"></i> ' . trans('LaravelLogger::laravel-logger.modals.shared.btnCancel'))->class(['btn', 'btn-outline', 'pull-left', 'btn-flat'])->type(['button']) ?>
+ <?php echo html()->button('<i class="fa ' . $actionBtnIcon . '" aria-hidden="true"></i> ' . $btnSubmitText)->type(['button']) ?>

im stuck at this line of code

array('class' => 'btn btn-' . $modalClass . ' pull-right btn-flat')

i dont know what to do this variable in "class"

foreach ($node->args as $arg_key => $arg){
  if (!$arg instanceof Arg) {
   continue;
  }
$argValue = $this->valueResolver->getValue($arg->value); // return nothing

im writing my own ValueResolver to process this line, here is where am i

public function extractConstantArrayTypeValue(ConstantArrayType $constantArrayType) : ?array
{
$keys = [];
foreach ($constantArrayType->getKeyTypes() as $i => $keyType) {
    $keys[$i] = $keyType->getValue();
}
$values = [];
foreach ($constantArrayType->getValueTypes() as $i => $valueType) {
    if ($valueType instanceof ConstantArrayType) {
        $value = $this->extractConstantArrayTypeValue($valueType);
    } elseif ($valueType instanceof ConstantScalarType) {
        $value = $valueType->getValue();
    } elseif (ClassNameFromObjectTypeResolver::resolve($valueType) !== null) {
        continue;
    } elseif ($valueType instanceof IntersectionType) { 
        //array('class' => 'btn btn-' . $modalClass . ' pull-right btn-flat')   
        foreach ($valueType->getTypes() as $j => $subType) {
            if ($subType instanceof AccessoryNonFalsyStringType) {
                //i dont know how to resolve this
                continue;
            }
            $value = $subType->getValue(); 
            // Call to undefined method PHPStan\Type\StringType::getValue()
        }
    } else {
        return null;
    }
    $values[$keys[$i]] = $value;
}
return $values;
}

what should i do to make this

'class' => 'btn btn-' . $modalClass . ' pull-right btn-flat'

into this

->class(['btn', 'btn-'. $modalClass, 'pull-right', 'btn-flat'])

Any help will be great

I'm trying to write a rector set for my project to migrate from laravel/collective to spatie/html currently my refactored code looks like this

- <?php echo \Collective\Html\FormFacade::button('<i class="fa fa-fw fa-close" aria-hidden="true"></i> ' . trans('LaravelLogger::laravel-logger.modals.shared.btnCancel'), array('class' => 'btn btn-outline pull-left btn-flat', 'type' => 'button', 'data-dismiss' => 'modal' )) ?>
- <?php echo \Collective\Html\FormFacade::button('<i class="fa ' . $actionBtnIcon . '" aria-hidden="true"></i> ' . $btnSubmitText, array('class' => 'btn btn-' . $modalClass . ' pull-right btn-flat', 'type' => 'button', 'id' => 'confirm' )) ?>
+ <?php echo html()->button('<i class="fa fa-fw fa-close" aria-hidden="true"></i> ' . trans('LaravelLogger::laravel-logger.modals.shared.btnCancel'))->class(['btn', 'btn-outline', 'pull-left', 'btn-flat'])->type(['button']) ?>
+ <?php echo html()->button('<i class="fa ' . $actionBtnIcon . '" aria-hidden="true"></i> ' . $btnSubmitText)->type(['button']) ?>

im stuck at this line of code

array('class' => 'btn btn-' . $modalClass . ' pull-right btn-flat')

i dont know what to do this variable in "class"

foreach ($node->args as $arg_key => $arg){
  if (!$arg instanceof Arg) {
   continue;
  }
$argValue = $this->valueResolver->getValue($arg->value); // return nothing

im writing my own ValueResolver to process this line, here is where am i

public function extractConstantArrayTypeValue(ConstantArrayType $constantArrayType) : ?array
{
$keys = [];
foreach ($constantArrayType->getKeyTypes() as $i => $keyType) {
    $keys[$i] = $keyType->getValue();
}
$values = [];
foreach ($constantArrayType->getValueTypes() as $i => $valueType) {
    if ($valueType instanceof ConstantArrayType) {
        $value = $this->extractConstantArrayTypeValue($valueType);
    } elseif ($valueType instanceof ConstantScalarType) {
        $value = $valueType->getValue();
    } elseif (ClassNameFromObjectTypeResolver::resolve($valueType) !== null) {
        continue;
    } elseif ($valueType instanceof IntersectionType) { 
        //array('class' => 'btn btn-' . $modalClass . ' pull-right btn-flat')   
        foreach ($valueType->getTypes() as $j => $subType) {
            if ($subType instanceof AccessoryNonFalsyStringType) {
                //i dont know how to resolve this
                continue;
            }
            $value = $subType->getValue(); 
            // Call to undefined method PHPStan\Type\StringType::getValue()
        }
    } else {
        return null;
    }
    $values[$keys[$i]] = $value;
}
return $values;
}

what should i do to make this

'class' => 'btn btn-' . $modalClass . ' pull-right btn-flat'

into this

->class(['btn', 'btn-'. $modalClass, 'pull-right', 'btn-flat'])

Any help will be great

Share Improve this question edited Jan 31 at 14:17 Dmitry Vasilyev asked Jan 31 at 14:13 Dmitry VasilyevDmitry Vasilyev 314 bronze badges 2
  • You just need to split the single string into separate class segments and handle the variable-based part as its own item. Think of it as turning “btn btn-{{variable}} pull-right btn-flat” into an array where each class name, including the one with the variable, is a separate element. Then you can pass that array to your HTML builder and chain it properly. This approach keeps everything flexible, so additional classes or changes to the variable won’t break your layout. Once you do that, your migration to Spatie’s HTML methods should work smoothly. – Kamyar Safari Commented Jan 31 at 14:52
  • valueResolver returns array without 'class' item. {"type":"button","id":"confirm"} how do split when i dont even get it back? – Dmitry Vasilyev Commented Feb 3 at 9:13
Add a comment  | 

1 Answer 1

Reset to default 1

big thanks to TomasVotruba found another solution by getting array with nodeFinder and then transformConcatToStringArray()

$array = $this->nodeFinder->findFirstInstanceOf($node, Array_::class);
$class_args = [];
$class_items = new Array_();
foreach ($array->items as $arrayItem) {
    $arr_key_name = $arrayItem->key->value;
    if($arrayItem->value instanceof Concat){
        $class_array = $this->NodeTransformer->transformConcatToStringArray($arrayItem->value);
        foreach ($class_array->items as $key_row => $row){
            if($row->value instanceof Variable)
                continue;
            if(count($class_array->items) > $key_row && $class_array->items[$key_row+1]->value instanceof Variable){
                $class_items->items[] = new ArrayItem(new Concat($row->value, $class_array->items[$key_row+1]->value));
                continue;
            }
            $class_items->items[] = new ArrayItem($row->value);
        }
        $class_args[] = new Arg($class_items);
        $new_function = new MethodCall($new_function, $arr_key_name, $class_args);
    }
}

<?php echo html()->button('<i class="fa ' . $actionBtnIcon . '" aria-hidden="true"></i> ' . $btnSubmitText)->type(['button'])->id(['confirm'])->class(['btn', 'btn-' . $modalClass, 'pull-right', 'btn-flat']) ?>

本文标签: phpRector how to work with IntersectionTypeStack Overflow