driade / phpfmt8

PHP formatter for Sublime Text 4, with PHP 8 support.
BSD 3-Clause "New" or "Revised" License
45 stars 2 forks source link

Condition shorthand issue #34

Closed finalblast closed 1 year ago

finalblast commented 1 year ago

Before

<?php

class Message
{
    public function test()
    {
        $campaign_data = ['enabled' = false];
        $campaign->status = $campaign_data['enabled'] ? Campaign::STATUS_ACTIVE : Campaign::STATUS_PAUSED;

        return $campaign;
    }
}

After

<?php

class Test
{
    public function test()
    {
        $campaign_data = ['enabled' = false];
        $campaign->status = $campaign_data['enabled']?Campaign::STATUS_ACTIVE: Campaign::STATUS_PAUSED;

        return $campaign;
    }
}

Configs:

{
    "version": 4,
    "php_bin": "/opt/homebrew/opt/php@7.4/bin/php",
    "format_on_save": false,
    "autocomplete": true,
    "autoimport": true,
    "disable_auto_align": true,
    "indent_with_space": true,
    "passes":
    [
        "StripExtraCommaInArray",
        "RemoveSemicolonAfterCurly",
        "NewLineBeforeReturn",
        "AddMissingParentheses",
        "ReindentSwitchBlocks",
        "OrderAndRemoveUseClauses",
        "DocBlockToComment",
        "PSR2EmptyFunction",
        "StripSpaceWithinControlStructures",
        "ShortArray",
        "AutoSemicolon",
        "OrderMethod", // 2023-06-05
        "OrganizeClass", // 2023-06-05
        "OrderMethodAndVisibility", // 2023-06-05
        "SortUseNameSpace", // 2023-06-05
        "SpaceBetweenMethods", // 2023-06-05
        "TrimSpaceBeforeSemicolon", // 2023-06-05
        "DoubleToSingleQuote"
    ],
    "psr1": false,
    "psr2": true,
    "smart_linebreak_after_curly": true,
    "visibility_order": true,
    "yoda": false,
}
driade commented 1 year ago

Thank you. The fix for #29 made this up. It's already fix in the next release

finalblast commented 1 year ago

Thank you for the quick fix, Appreciate that. Since the latest fix, seem another issue happens:

Before

<?php

class Test
{
    public function saveTest()
    {
        $campaign = $api->getCampaign($campaign_id);
        $db_test = new Test();
        $db_test->name = $campaign->{CampaignFields::NAME};
        $db_test->status = $campaign->{CampaignFields::EFFECTIVE_STATUS};
        $db_test->budget = $campaign->{CampaignFields::LIFETIME_BUDGET};
        $db_test->save();
    }
}

After

<?php

class Test
{
    public function saveTest()
    {
        $campaign = $api->getCampaign($campaign_id);
        $db_test = new Test();
        $db_test->name = $campaign->{CampaignFields::NAME}
        $db_test->status = $campaign->{CampaignFields::EFFECTIVE_STATUS}
        $db_test->budget = $campaign->{CampaignFields::LIFETIME_BUDGET}
        $db_test->save();
    }
}

Please have a look @driade

driade commented 1 year ago

There you're, fixed. Thanks.

finalblast commented 1 year ago

@driade I think it makes another bigger issue.

Before

<?php

class Test
{
    public function saveTest()
    {
        $value = [
            'test' => 1,
            'another_test' => 2
        ];
        foreach (array_keys($value) as $array_key) {
            $report->{$array_key} = $value[$array_key];
        }
        $report->save();
    }
}

After

<?php

class Test
{
    public function saveTest()
    {
        $value = [
            'test' => 1,
            'another_test' => 2
        ];
        foreach (array_keys($value) as $array_key) {
            $report->{$array_key}; = $value[$array_key];
        }
        $report->save();
    }
}

Another case

<?php

class Test
{
    public function saveTest()
    {
        $user = {};
        $tests = $api->getTests();
        foreach ($tests as $key => $test) {
            $db_test[] = [
                'test' => $test->{'Id'},
                'another_test' => $user->another_test,
                'user_id' => $user->user_id,
                'blah' => $user->blah,
                'name' => $test->{'Name'},
                'status' => strtoupper($test->{'Status'}),
                'budget' => $test->{'BudgetType'} === 'DailyBudgetStandard' ? $test->{'DailyBudget'} : 0,
                'updated_at' => now()
            ];
        }
    }
}

Becomes

<?php

class Test
{
    public function saveTest()
    {
        $user = {}
        $tests = $api->getTests();
        foreach ($tests as $key => $test) {
            $db_test[] = [
                'test' => $test->{'Id'};,
                'another_test' => $user->another_test,
                'user_id' => $user->user_id,
                'blah' => $user->blah,
                'name' => $test->{'Name'};,
                'status' => strtoupper($test->{'Status'};),
                'budget' => $test->{'BudgetType'}; === 'DailyBudgetStandard' ? $test->{'DailyBudget'}; : 0,
                'updated_at' => now()
            ];
        }
    }
}
driade commented 1 year ago

Thank you very much @finalblast . Patch uploaded. The more cases we've the more confident we're.

finalblast commented 1 year ago

Thanks for quick fix @driade

There is also 1 more issue I wanna to ask:

<?php

class Test
{
    public function array(array $rows)
    {
        if (count($rows) > 0) {
            foreach ($rows as $row) {
                // Save DB
            }
        }
    }
}

Becomes

<?php

class Test
{
    function [array $rows) {
        if (count($rows) > 0) {
            foreach ($rows as $row) {
                // Save DB
            }
        }
    }
}

Another case

<?php

class Test
{
    public function array($rows)
    {
        if (count($rows) > 0) {
            foreach ($rows as $row) {
                // Save DB
            }
        }
    }
}

Becomes

<?php

class Test
{
    function [$rows] {
        if (count($rows) > 0) {
            foreach ($rows as $row) {
                // Save DB
            }
        }
    }
}

Thank you.

driade commented 1 year ago

Hi, thanks. May you open a separate issue for this case?

El vie, 9 jun 2023, 10:34, Nam (Nick) N. HUYNH @.***> escribió:

Thanks for quick fix @driade https://github.com/driade

There is also 1 more issue I wanna to ask:

<?php class Test { public function array(array $rows) { if (count($rows) > 0) { foreach ($rows as $row) { // Save DB } } } }

Becomes

<?php class Test { function [array $rows) { if (count($rows) > 0) { foreach ($rows as $row) { // Save DB } } } }

Thank you.

— Reply to this email directly, view it on GitHub https://github.com/driade/phpfmt8/issues/34#issuecomment-1584187276, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLNWSGCRASQSNIOQJPH5CLXKLN3DANCNFSM6AAAAAAY2VGU2M . You are receiving this because you were mentioned.Message ID: @.***>

driade commented 1 year ago

Hello again @finalblast .

"array" in a reserved word https://www.php.net/manual/en/reserved.keywords.php so you can´t use it a a function name

Captura de pantalla 2023-06-09 a las 20 09 51
finalblast commented 1 year ago

@driade There is a repository that is required to implement the interface that the function name was an array. You can look here: https://github.com/SpartnerNL/Laravel-Excel/blob/3.1/src/Concerns/ToArray.php

So in the code, we cannot format the file.

finalblast commented 1 year ago

I think it is because of ShortArray config?

driade commented 1 year ago

Yes, it's because of the ShortArray, which you can disable of course.

Anyway I now understand you needs, and I think I can handle it. Give me a second.

driade commented 1 year ago

There you're, patch uploaded!

finalblast commented 1 year ago

Thanks @driade , it is almost perfect but still 3 issues:

<?php

class Test
{
    public function array($rows)
    {
        if (count($rows) > 0) {
            foreach ($rows as &$row) {
                // Save DB
            }
        }
    }
}

Becomes

<?php

class Test
{
    function array($rows) {
        if (count($rows) > 0) {
            foreach ($rows as  &$row) {
                // Save DB
            }
        }
    }
}

Issues:

  1. It should keep public word at the beginning.
  2. It should keep the { in the new line.
  3. It shouldn't add 1 more space before &$row.
driade commented 1 year ago

Thanks a lot, fixed

finalblast commented 1 year ago

@driade Thank you. I think it is almost perfect, only 1 case left:

<?php

class Test
{
    public function array($rows)
    {
        if (!isset($groups[$test['category']])) {
            $children[$test['category']] = [];
            $groups[$test['category']] = [
                'id' => $test['category'],
                'type' => 'group',
                'label' => $test['category'],
                'children' => &$children[$test['category']]
            ];

            $options[] = $groups[$test['category']];
        }
    }
}

Becomes

<?php

class Test
{
    public function array($rows)
    {
        if (!isset($groups[$test['category']])) {
            $children[$test['category']] = [];
            $groups[$test['category']] = [
                'id' => $test['category'],
                'type' => 'group',
                'label' => $test['category'],
                'children' =>  &$children[$test['category']]
            ];

            $options[] = $groups[$test['category']];
        }
    }
}

You can notice an addition space in 'children' => &$children[$test['category']]

driade commented 1 year ago

There you're, fixed, thanks!

finalblast commented 1 year ago

Wonderful. Thank you very much, all good now.