Search code examples
phplaravelcollectiverector

Rector how to work with IntersectionType?


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


Solution

  • 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']) ?>