vysker / vscode-php-formatter

Visual Studio Code extension. A wrapper for the Sensiolabs PHP CS Fixer. Analyzes some PHP source code and tries to fix coding standards issues (PSR-1 and PSR-2 compatible).
MIT License
94 stars 11 forks source link

PHP CS Fixer 2.0.x not working #15

Open vysker opened 7 years ago

vysker commented 7 years ago

A lot of users are reporting that this extension does not work with PHP CS Fixer 2.0.x. So this issue will be here to accumulate as much info about the problem at hand.

If you are running into this issue as well, please post the following info:

I will try to work out a solution soon enough. Time might be an issue around all the upcoming festivities, however.

Thanks!

Join-Git-Hub commented 7 years ago

OK, Thanks, here are my current Settings :pencil2:

OS.

Windows 7 Home Premium

VSCode version.

Version 1.8.0

PHP CS Fixer version.

2.0.0

PHPFormatter version

0.2.4

PHPFormatter user settings.

{
    "window.zoomLevel": 1.5,
    "editor.lineHeight": 28,
    "telemetry.enableTelemetry": false,
    "telemetry.enableCrashReporter": false,
    "php.validate.executablePath": "c:/xampp/php/php.exe",
    "phpformatter.phpPath": "C:/xampp/php/php.exe",
    "phpformatter.pharPath": "c:/xampp/php/php-cs-fixer.phar",
    "phpformatter.logging": true
}

PHPFormatter logging output (use phpformatter.logging: true).

log

If possible, a snippet or screenshot of the code that needs formatting.

class ControllerErrorPermission extends Controller {
    public function index() {
        $this->load->language('error/permission');

        $this->document->setTitle($this->language->get('heading_title'));

        $data['heading_title'] = $this->language->get('heading_title');

        $data['text_permission'] = $this->language->get('text_permission');

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('error/permission', 'token=' . $this->session->data['token'], 'SSL')
        );

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('error/permission.tpl', $data));
    }

    public function check() {
        if (isset($this->request->get['route'])) {
            $route = '';

            $part = explode('/', $this->request->get['route']);

            if (isset($part[0])) {
                $route .= $part[0];
            }

            if (isset($part[1])) {
                $route .= '/' . $part[1];
            }

            $ignore = array(
                'common/dashboard',
                'common/login',
                'common/logout',
                'common/forgotten',
                'common/reset',
                'error/not_found',
                'error/permission',
                'dashboard/order',
                'dashboard/sale',
                'dashboard/customer',
                'dashboard/online',
                'dashboard/map',
                'dashboard/activity',
                'dashboard/chart',
                'dashboard/recent'
            );

            if (!in_array($route, $ignore) && !$this->user->hasPermission('access', $route)) {
                return new Action('error/permission');
            }
        }
    }
}
sergiufp commented 7 years ago

I am experiencing a similar error:

PHPFormatter: Full command being executed: "E:\Web\UwAmp\bin\php\php-7.1.0-Win32-VC14-x86\php.exe" "E:\Web\UwAmp\bin\php\php-7.1.0-Win32-VC14-x86\phpcs.phar" fix "C:\Users\sergi\AppData\Local\Temp\phpfmt-7716K5NoyoCpqMqO.tmp" --level=psr2
PHPFormatter: Logging PHP-CS-Fixer command stdout result
PHPFormatter: ERROR: The file "fix" does not exist.

OS: Windows 10 Pro x64 VSCode version: 1.7.2 PHP CS Fixer version: 2.0.0 PHPFormatter version: 0.2.4 PHPFormatter user settings:

"phpformatter.pharPath": "E:\\Web\\UwAmp\\bin\\php\\php-7.1.0-Win32-VC14-x86\\phpcs.phar",
"phpformatter.phpPath": "E:\\Web\\UwAmp\\bin\\php\\php-7.1.0-Win32-VC14-x86\\php.exe",
"phpformatter.logging": true,

Later edit: issue persists after updating VSCode to version 1.8.0

yaliv commented 7 years ago
Join-Git-Hub commented 7 years ago

@yaliv

PHPFormatter logging output: I don't know how to see the logging output.

Klick HELP -> Toggle Developer Tool -> Console

yaliv commented 7 years ago

OK, here is the output in the console.

(I use the same versions as here).

Using Composer:

[Extension Host] PHPFormatter: Creating temp file.
[Extension Host] PHPFormatter: Tempfile fd: 61
[Extension Host] PHPFormatter: Tempfile name: /tmp/phpfmt-5947C8m6dUBQqw4n.tmp
[Extension Host] PHPFormatter: Writing current document content to temp file. Until VSCode will have a way of querying encoding, utf8 will be used for reading and writing.
[Extension Host] PHPFormatter: Full command being executed: php-cs-fixer fix "/tmp/phpfmt-5947C8m6dUBQqw4n.tmp" --level=psr2
[Extension Host] PHPFormatter: Logging PHP-CS-Fixer command stderr result
[Extension Host] PHPFormatter: 

  [Symfony\Component\Console\Exception\RuntimeException]
  The "--level" option does not exist.

fix [--path-mode PATH-MODE] [--allow-risky ALLOW-RISKY] [--config CONFIG] [--dry-run] [--rules RULES] [--using-cache USING-CACHE] [--cache-file CACHE-FILE] [--diff] [--format FORMAT] [--] [<path>]...

[Extension Host] PHPFormatter: Reading temp file content.
[Extension Host] PHPFormatter: Replacing editor content with formatted code.
[Extension Host] PHPFormatter: Document successfully formatted (19 lines).

Using php-cs-fixer.phar file:

[Extension Host] PHPFormatter: Creating temp file.
[Extension Host] PHPFormatter: Tempfile fd: 61
[Extension Host] PHPFormatter: Tempfile name: /tmp/phpfmt-5947Q0cGP1CpJXtR.tmp
[Extension Host] PHPFormatter: Writing current document content to temp file. Until VSCode will have a way of querying encoding, utf8 will be used for reading and writing.
[Extension Host] PHPFormatter: Full command being executed: "/opt/lampp/bin/php" "$HOME/phar/php-cs-fixer.phar" fix "/tmp/phpfmt-5947Q0cGP1CpJXtR.tmp" --level=psr2
[Extension Host] PHPFormatter: Logging PHP-CS-Fixer command stderr result
[Extension Host] PHPFormatter: 

  [RuntimeException]
  The "--level" option does not exist.

fix [--path-mode="..."] [--allow-risky="..."] [--config="..."] [--dry-run] [--rules="..."] [--using-cache="..."] [--cache-file="..."] [--diff] [--format="..."] [path1] ... [pathN]

[Extension Host] PHPFormatter: Reading temp file content.
[Extension Host] PHPFormatter: Replacing editor content with formatted code.
[Extension Host] PHPFormatter: Document successfully formatted (19 lines).

Sample case:
I copied a code snippet from a PDF document into VS Code, the code was copied without indentation. After trying to use PHPFormatter to format my code, nothing changed.

My code snippet:

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\Room;
class RoomsController extends Controller
{
public function actionCreate()
{
$model = new Room();
$modelCanSave = false;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$modelCanSave = true;
}return $this->render('create', [
'model' => $model,
'modelSaved' => $modelCanSave
]);
}
}
ncwgf commented 7 years ago

@yaliv According to readme of php-cs-fixer https://github.com/FriendsOfPHP/PHP-CS-Fixer#usage php php-cs-fixer.phar fix /path/to/project --rules=@PSR2 --level should be change to --rules=@PSR2

and this extension will add --level=2 by default in setting phpformatter.arguments

just found it was discussed at #13

yaliv commented 7 years ago

@ncwgf

OK, it works with --rules argument.

Using @PSR2:

<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Room;

class RoomsController extends Controller
{
    public function actionCreate()
    {
        $model = new Room();
        $modelCanSave = false;
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $modelCanSave = true;
        }
        return $this->render('create', [
'model' => $model,
'modelSaved' => $modelCanSave
]);
    }
}

Using @Symfony:

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Room;

class RoomsController extends Controller
{
    public function actionCreate()
    {
        $model = new Room();
        $modelCanSave = false;
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $modelCanSave = true;
        }

        return $this->render('create', [
'model' => $model,
'modelSaved' => $modelCanSave,
]);
    }
}

The most difficult to format is the indentation for multi-line array. Any solution?

ncwgf commented 7 years ago

it's php-cs-fixer problem and seem it still not ... ready https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/1438

NewFuture commented 7 years ago

https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/v2.0.0/UPGRADE.md

😹

vysker commented 7 years ago

Thank you all for reporting in. I have been struggling with maintaining support for both versions of PHP CS Fixer for a while now. So in a next update I will be integrating PHP CS Fixer 2.0.0 to circumvent all this.

I will be leaving this issue open so people will have a place to go in the mean time.

lichnow commented 7 years ago

[Symfony\Component\Filesystem\Exception\IOException]
Failed to write file ".php_cs.cache", "no reason available".

My setting { "phpcs.standard": "PSR2", "phpformatter.phpPath": "/usr/local/bin/php", "phpformatter.composer": true, "phpformatter.logging": true, "phpformatter.arguments": [ "--rules=@PSR2" ] }

error
ajkopinga commented 7 years ago

@lichnow

I encountered the same 'Failed to write file ".php_cs.cache"' error. Caching only seems useful when fixing multiple files at once, so I skipped caching by adding '--using-cache false' to the phpformatter.arguments setting.

This is a temporary fix to make the formatter work.

kadevland commented 7 years ago

ai can't wowrking at all this extention

kadevland commented 7 years ago

when a save the file with debuug on i have "spawn php EMFILE"

kadevland commented 7 years ago

for me i have change addon. So it's done for me

lesydat commented 7 years ago

I changed this setting from default value

  "phpformatter.arguments": [
    "--level=psr2"
  ],

to this

"phpformatter.arguments": []

It work well

elisecode247 commented 7 years ago

@lesydat's solution worked for me.

Tommixoft commented 7 years ago

Tried every possible solution - nothing helps. I call this extension bullshit. If it's not compatible with v2 - so write that in documentation.

nucklearproject commented 7 years ago

@lesydat Work fine now...

dxkite commented 7 years ago

change argument --rules=@psr2 to --rules=@PSR2 will work well. this is my setting

{
    "phpformatter.composer": true,
    "phpformatter.logging": true,
    "phpformatter.arguments": ["--rules=@PSR2"]
 }

@lesydat

bardware commented 6 years ago

"--level=psr2"

"--rules=@PSR2"