Skip to content

printParameters is not taking into account the rest of the method to determine line length for wrapping #77

@Eydamos

Description

@Eydamos

Version: 3.5.2

Bug Description

The method printParameters in the Printer class checks if the length of the arguments is greater than (new Dumper)->wrapLength. But this only checks if the list of parameters exceeds the 120 character limit but ignores the string length of the rest of the method head like the visibility, method name, function keyword and the return type

Steps To Reproduce

Example code:

$file = new PhpFile();
$namespace = $file->addNamespace('Foo');
$class = $namespace->addClass('Bar');
$method = $class->addMethod('baz')
                ->setPublic()
                ->setReturnType('string');
$method->addParameter('lorem')
       ->setType('string');
$method->addParameter('ipsum')
       ->setType('string');
$method->addParameter('dolor')
       ->setType('string');
$method->addParameter('sit')
       ->setType('string');
$method->addParameter('amet')
       ->setType('string');
$method->addParameter('foo')
       ->setType('string');
$method->addParameter('bar')
       ->setType('string');

echo (new PsrPrinter())->printFile($file);

Will print the following:

<?php

namespace Foo;

class Bar
{
    public function baz(string $lorem, string $ipsum, string $dolor, string $sit, string $amet, string $foo, string $bar): string
    {
    }
}

The function baz now has a line length of 130 characters which is too long for PSR2 and PSR12

Expected Behavior

I expect, that the rest of the method head is also taken into account when checking the line length so the following output would be generated which is PSR2 and PSR12 valid:

<?php

namespace Foo;

class Bar
{
    public function baz(
        string $lorem,
        string $ipsum,
        string $dolor,
        string $sit,
        string $amet,
        string $foo,
        string $bar
    ): string {
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions