vscode-phpfmt
vscode-phpfmt copied to clipboard
Constant arrays on classes causes all keywords before `function` to be removed
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
}
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()
{
}
}
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
I'm also having this issue. Is there a fix yet?
Same here. Can we at least get the name of the transformation that does this so we can disable it?
Almost a year passed, do we have any info on this issue? Or is dropping phpfmt and using another formatter the only option?
I too found this bug the hard way. However quick test indicates this is enough to disable it:
{
"phpfmt.exclude": [
"PSR2ModifierVisibilityStaticOrder"
]
}
Please add a permanent fix for it. It can screw up a perfectly working code in production.
I'll take a look.
There you're, it's fixed in the latest release.
How do I get the update into my version that I use in VSCode? (phpfmt 1.1.9)
@Christian-Rinne the very fact it's I don't. cc @kokororin ?
@Christian-Rinne may you please try the command "phpfmt: Upgrade fmt.phar or fmt.stub.php"?
I've done it, and it showed that it was successful, but unfortunately the bug still persists (I've also restarted VSCode).
Sorry but I can't reproduce the bug. May you reinstall the plugin?
Please try version 1.1.10, it contains the latest fmt
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.
Thanks, let me check with the sample given
There you're, patched uploaded. Please try upgrading via "phpfmt: Upgrade fmt.phar or fmt.stub.php" and let's see.
Yes! it works :) Thank you very much