vscode-phpfmt icon indicating copy to clipboard operation
vscode-phpfmt copied to clipboard

Constant arrays on classes causes all keywords before `function` to be removed

Open wisaac407 opened this issue 6 years ago • 7 comments

If you have a class with a constant on it that is an array, static functions will get re-written to not be static (also, the public keyword gets removed). E.g:

this

<?php

class Bug {
    const BROKEN = array();

    public static function test() {

    }
}

Will get re-written into this:

<?php

class Bug {
    const BROKEN = array();

    function test() {

    }
}

It seems to remove all keywords before the function keyword:

<?php

class Bug {
    const BROKEN = array();

    private function test() {

    }
}

Will get re-written into this:

<?php

class Bug {
    const BROKEN = array();

    function test() {

    }
}

However, this works:

<?php

const WORKS = array();

class Bug {

    public static function test() {

    }
}

These are the settings I'm using:

{
    "phpfmt.exclude": [
        "PSR2CurlyOpenNextLine",
    ],
    "phpfmt.passes": [
        "AlignDoubleArrow"
    ],
    "phpfmt.psr2": true
}

wisaac407 avatar Jul 05 '18 14:07 wisaac407

Using traits on a class will sometimes cause this bug to appear as well. When using multiple traits and curly braces (e.g. for aliasing or resolving method conflicts) this will cause public static to be removed.

<?php

class Bug 
{
    use FooTrait, BarTrait {}

    public static function test()
    {
    }
}

mattkelliher avatar Aug 21 '18 23:08 mattkelliher

this is really annoying, only the first function stays the same, any other function all keywords get removed. I have a const array and having the same issue

yousufmj avatar Sep 27 '18 13:09 yousufmj

I'm also having this issue. Is there a fix yet?

josjojo avatar Dec 22 '18 15:12 josjojo

Same here. Can we at least get the name of the transformation that does this so we can disable it?

loderunner avatar Feb 27 '19 11:02 loderunner

Almost a year passed, do we have any info on this issue? Or is dropping phpfmt and using another formatter the only option?

demolitions avatar May 30 '19 07:05 demolitions

I too found this bug the hard way. However quick test indicates this is enough to disable it:

{
    "phpfmt.exclude": [
        "PSR2ModifierVisibilityStaticOrder"
    ]
}

ianleeder avatar Jun 07 '22 11:06 ianleeder

Please add a permanent fix for it. It can screw up a perfectly working code in production.

vamaship-saud-qureshi avatar Jun 22 '22 06:06 vamaship-saud-qureshi

I'll take a look.

driade avatar Jun 28 '23 11:06 driade

There you're, it's fixed in the latest release.

driade avatar Jun 28 '23 11:06 driade

How do I get the update into my version that I use in VSCode? (phpfmt 1.1.9)

Christian-Rinne avatar Jun 28 '23 12:06 Christian-Rinne

@Christian-Rinne the very fact it's I don't. cc @kokororin ?

driade avatar Jun 28 '23 14:06 driade

@Christian-Rinne may you please try the command "phpfmt: Upgrade fmt.phar or fmt.stub.php"?

Captura de pantalla 2023-06-28 a las 22 00 48

driade avatar Jun 28 '23 20:06 driade

I've done it, and it showed that it was successful, but unfortunately the bug still persists (I've also restarted VSCode).

Christian-Rinne avatar Jun 29 '23 16:06 Christian-Rinne

Sorry but I can't reproduce the bug. May you reinstall the plugin?

driade avatar Jun 29 '23 16:06 driade

Please try version 1.1.10, it contains the latest fmt

kokororin avatar Jun 30 '23 01:06 kokororin

Thank you so much for your effort and help! I have just upgraded phpfmt to 1.1.10 and restarted VSCode, but the problem still persists. I also ran "phpfmt: Upgrade fmt.phar or fmt.stub.php" again (and restarted afterwards).

To help you track down the error, here are some data with which you might be able to reproduce the bug:

  • As far as I know, it only occurs with functions named print() within classes.
<?php

use classes\Printer;
use classes\Sql;

class DocumentManager
{
    protected static $documentPaths = [];

    public static function initDocumentPaths()
    {
        self::$documentPaths = [
            'sepa-mandate'           => \Auth::user()->getFolder() . '/sepa_mandate',
            'cash-accounting-report' => \Auth::user()->getS3TmpFolder() . '/pdf-export',
            'vat-report-eu'          => \Auth::user()->getS3TmpFolder() . '/pdf-export',
            'address-labels'         => \Auth::user()->getS3TmpFolder() . '/pdf-export',
            'export-backup'          => \Auth::user()->getS3TmpFolder() . '/pdf-export',
        ];
    }

    public static function getDocumentPath($type)
    {
        // Initialize document paths if not done yet
        if (empty(self::$documentPaths))
        {
            self::initDocumentPaths();
        }

        return self::$documentPaths[$type];
    }

    // after save (formatting) "public static" is gone
    public static function print($type, $data = array(), $filename = null)
    {
        ...
    }

Here are my VSCode settings.json:

{
  "settings": {
    "editor.accessibilitySupport": "off",
    "editor.bracketPairColorization.enabled": false,
    "editor.codeLens": false,
    "editor.fontFamily": "Menlo, SF Mono, JetBrains Mono, Source Code Pro, ProggyVector, serif",
    "editor.fontSize": 12.5,
    "editor.formatOnPaste": false,
    "editor.formatOnSave": true,
    "editor.formatOnType": false,
    "editor.insertSpaces": true,
    "editor.inlineSuggest.enabled": true,
    "editor.lineHeight": 1.7,
    "editor.mouseWheelScrollSensitivity": 0.2,
    "editor.tabSize": 4,
    "editor.renderWhitespace": "all"
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "[php]": {
      "editor.defaultFormatter": "kokororin.vscode-phpfmt",
      "editor.formatOnSave": true,
      "editor.formatOnType": false,
      "editor.formatOnPaste": false,
      "editor.tabSize": 4
    },
    "phpfmt.php_bin": "/usr/local/bin/php",
    "phpfmt.psr2": true,
    "phpfmt.passes": ["AllmanStyleBraces"],
    "phpfmt.enable_auto_align": true,
    "editor.tokenColorCustomizations": {
      "textMateRules": [...]
    },
    "explorer.compactFolders": false,
    "explorer.confirmDelete": false,
    "explorer.openEditors.visible": 0,
    "git.mergeEditor": true,
    "json.schemas": [],
    "workbench.colorCustomizations": {
      "editor.background": "#171c23",
      "editorIndentGuide.activeBackground": "#2e3846",
      "editorIndentGuide.background": "#1f262f"
    },
    "workbench.colorTheme": "One Dark Pro Darker",
    "workbench.editor.wrapTabs": true,
    "workbench.iconTheme": "vsc-jetbrains-icons-enhanced",
    "workbench.startupEditor": "none",
  }
}

when i add the following rule, the bug is gone:

"phpfmt.exclude": ["PSR2ModifierVisibilityStaticOrder"],

I hope you can find the bug. Thank you so far! If further data is needed, I'll be glad to provide it.

Christian-Rinne avatar Jun 30 '23 08:06 Christian-Rinne

Thanks, let me check with the sample given

driade avatar Jun 30 '23 08:06 driade

There you're, patched uploaded. Please try upgrading via "phpfmt: Upgrade fmt.phar or fmt.stub.php" and let's see.

driade avatar Jun 30 '23 09:06 driade

Yes! it works :) Thank you very much

Christian-Rinne avatar Jun 30 '23 09:06 Christian-Rinne