Giter VIP home page Giter VIP logo

mansoorkhan96 / php-cs-fixer Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 4.0 4.24 MB

Fastest PHP CS Fixer (Prettier for PHP) extension for VS Code with zero config

Home Page: https://marketplace.visualstudio.com/items?itemName=mansoorkhan96.php-cs-fixer

License: MIT License

JavaScript 45.98% PHP 54.02%
php-cs-fixer vscode vscode-extension visual-studio visual-studio-code php php8 php81 prettier prettier-php

php-cs-fixer's Introduction

VsCode PHP CS Fixer

This extension requires almost zero configuration to format .php files. It uses v3.59.3 of cs.symfony.com by default but that's easily replaceable. As long as PHP 7+ is installed on your system and in your PATH, the extension should work out of the box.

demo

Extension Optional Settings

This extension contributes the following settings:

  • php-cs-fixer.toolPath: The path to the php-cs-fixer tool (default: "")
  • php-cs-fixer.useCache: Use a cache file when fixing files (--using-cache) (default: true)
  • php-cs-fixer.allowRisky: Determines whether risky rules are allowed (--allow-risky) (default: false)
  • php-cs-fixer.config: Path to a config file (--config) (default: "WorkspaceFolder/.php-cs-fixer.php" OR "WorkspaceFolder/.php-cs-fixer.dist.php")
  • php-cs-fixer.rules: Rules to use when fixing files (--rules) (default: "@PSR12,@PSR1,@PSR2,@Symfony,-yoda_style")
  • php-cs-fixer.fixOnSave: Runs fix command on save (default: true)
  • php-cs-fixer.preset: Use a preset from: laravel, PER, PSR12, symfony. You should remove it if you want to use a config file.
  • php-cs-fixer.phpCmd: Specify path to PHP command (default: "php").

Adding PHP CS Fixer from a composer dependency

You can require PHP CS Fixer as composer dependency in your project and the extension will load it automatically for you.

If extension could not find/load the binary from /vendor/bin/php-cs-fixer, it would default to the binary included in extension.

Manually adding PHP CS Fixer binary file?

Download the binary file and provide the file path to the extension.

Open settings.json file (Ctrl + Shift + P) and add the following setting:

"php-cs-fixer.toolPath": "path\\php-cs-fixer.phar",

On Windows:

"php-cs-fixer.toolPath": "C:\\Users\\username\\.vscode\\php-cs-fixer.phar",

Config File

This extension formats .php files based on specified rules. Commonly these rules are defined in a .php-cs-fixer.dist.php OR .php-cs-fixer.php config file inside your project root path. The extension would try to pick a config file with above filenames, if not found it uses default rules.

You can also define a global config file which can be used accross all projects.

Open settings.json file (Ctrl + Shift + P) and add the following setting:

"php-cs-fixer.config": "/full/config/file/path"

On Windows:

"php-cs-fixer.config": "C:\\Users\\username\\.vscode\\.php-cs-fixer.php"

Path to PHP Command

You can specify path to PHP Command using the config.

"php-cs-fixer.phpCmd": "/Users/mansoor/Library/Application Support/Herd/bin/php",

Not sure which rules to add?

Try out following config rules. Try to remove a rule and observe the changes.

<?php

$config = new PhpCsFixer\Config();

return $config
    ->setRules([
        '@PSR12' => true,
        'new_with_braces' => false,
        'array_indentation' => true,
        'array_syntax' => ['syntax' => 'short'],
        'combine_consecutive_unsets' => true,
        'multiline_whitespace_before_semicolons' => true,
        'single_quote' => true,
        'blank_line_before_statement' => true,
        'braces' => [
            'allow_single_line_closure' => true,
        ],
        'concat_space' => ['spacing' => 'one'],
        'declare_equal_normalize' => true,
        'function_typehint_space' => true,
        'include' => true,
        'lowercase_cast' => true,
        'no_multiline_whitespace_around_double_arrow' => true,
        'no_spaces_around_offset' => true,
        'no_unused_imports' => true,
        'no_whitespace_before_comma_in_array' => true,
        'no_whitespace_in_blank_line' => true,
        'object_operator_without_whitespace' => true,
        'single_blank_line_before_namespace' => true,
        'ternary_operator_spaces' => true,
        'trailing_comma_in_multiline' => true,
        'trim_array_spaces' => true,
        'unary_operator_spaces' => true,
        'binary_operator_spaces' => true,
        'whitespace_after_comma_in_array' => true,
        'single_trait_insert_per_statement' => false,
    ])
    ->setLineEnding("\n");

You can find the complete rule set here

php-cs-fixer's People

Contributors

finn-matti avatar mansoorkhan96 avatar sanketgandhi876 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

php-cs-fixer's Issues

Support overwriting the PHP command

This could be done in two ways:

  1. Either by letting me specify a path to the php executable to use
  2. By letting me specify the command as a string

At the moment this always runs php-cs-fixer like this (if paht of the fixer is set at least):

php <pathToFixer>/php-cs-fixer fix <otherOptions> <file>

I'd like the option to specify the php portion.

How to set the path

Hi

I don't know how to set the path to php-cs-fixer file

following this suggestion:
Open settings.json file (Ctrl + Shift + P) and add the following setting:
"php-cs-fixer.toolPath": "path\php-cs-fixer.phar",

if I push (Ctrl + Shift + P) I cannot set this line (llok at the image)

"php-cs-fixer.toolPath": "/usr/local/bin/php-cs-fixer",

11

I've used the globally solution on my local debian server (I use sftp)
https://cs.symfony.com/doc/installation.html

wget https://cs.symfony.com/download/php-cs-fixer-v3.phar -O php-cs-fixer
sudo chmod a+x php-cs-fixer
sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer

111
I tried to set /usr/local/bin/php-cs-fixer here but I get an error during the formatting

can you help me how to use your extension?

thanks

(PR PROVIDED) Error: spawn ENOENT on MacOS and Pop!_OS

I've used this extension for a while on my iMac and it has worked fine, but when trying to use it on my Linux machine running Pop!_OS I'd never been able to get it to work, always getting an ENOENT error when the child process tried to spawn.

For whatever reason the php-cs-fixer file could never be found, regardless of whether I used a full or absolute path to my local copy, or whether I used the included php-cs-fixer and so on.

After getting fed up I decided to get to the bottom of this issue, and found this Stack Overflow response which suggested I try setting the shell option to true.

Lo and behold after adding it to the extension code it actually worked and the error completely vanished. I have no idea why this was the case and what difference adding this option makes exactly, but the fact it worked was enough for me.

A couple of days later I was trying to understand the issue in more depth with a collegue and seeing if I could reproduce the issue on his machine and fix it using the same solution before I submitted a PR here of any kind.

Bemusingly it worked out of the box on their machine, but I somehow managed to get the extension not working on my Mac, even after reinstalling, clearing caches etc.

I added the shell: true option to the source code and the error vanished, the script ran perfectly just as it was doing before...I really wish I knew how.

I'm not too familiar with node or whether setting this option has any knock on effects I don't know about, but regardless I shall submit a PR with this change if it's an acceptable one.

How to use the extension with Docker?

Hello!
I want to run the fixer from a docker container, but if I set docker-compose exec -T container-name vendor/bin/phing php-cs-fixer in Tool path it doesn't work.

Can I change a path of environment or a path to run the fixer from the container?
Thanks.

passing full dir path to php-cs-fixer Finder's in() method FAILs

i run

VSCODE
	Version: 1.77.1
	Commit: b7886d7461186a5eac768481578c1d7ca80e2d21
	Date: 2023-04-04T23:20:37.202Z
	Electron: 19.1.11
	Chromium: 102.0.5005.196
	Node.js: 16.14.2
	V8: 10.2.154.26-electron.0
	OS: Linux x64 6.2.9-200.fc37.x86_64
	Sandboxed: No

php-cs-fixer -V
	PHP CS Fixer 3.16.0 BoY42 by Fabien Potencier and Dariusz Ruminski.
	PHP runtime: 8.2.4

with

Name: PHP CS Fixer
Id: mansoorkhan96.php-cs-fixer
Description: PHP CS Fixer extension for VS Code with zero config
Version: 1.0.2
Publisher: mansoorkhan96
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=mansoorkhan96.php-cs-fixer

for an app dir structure

tree -d -L 1 myapp
	myapp
	├── wp-admin
	├── wp-content
		...
		├── themes
			...
			├── myTheme
				├── js
				├── media
				├── scss
				└── views
	└── wp-includes

with this Finder config

cat .php-cs-fixer.php
	$finder = PhpCsFixer\Finder::create()
		->in(__DIR__)
	;
...

on edit/save works as expected.

i want to limit fixing to ONLY *.php files anywhere under the myapp->wp-admin->wp-content->themes->myTheme path.

but, passing that dir's full path to in() method

	cat .php-cs-fixer.php
		$finder = PhpCsFixer\Finder::create()
---			->in(__DIR__)
+++			->in('/path/to/myapp/wp-content/themes/myTheme')
		;

on format exec it fails with error in VSC

There was an error while running php-cs-fixer. Check the Developer Tools console for more information.

i see nothing in Developer Tools console ...

bug, or is my usage wrong?

Specify multiple config files

Hello,

Thanks for this extension, it works like a charm and is currently the only one that works for me. However, I would suggest adding the possibility to specify multiple config files as the php-cs-fixer config file does not always have the same name across various projects (eg: '.php-cs-fixer.php|php-cs-fixer.dist.php' would first look for .php-cs-fixer.php and then php-cs-fixer.dist.php if the first one does not exist.
Do you think that would be possible?

Extension format on save does not run if vscode format on save in enabled

Hey @mansoorkhan96 👋🏻

Thanks for the extension! This is the only extension that worked for me :)

I configured php-cs-fixer.fixOnSave but on saving php file, it does not format it. After checking code, there is condition which check if editor.formatOnSave is disabled or not. If it is disabled then only extension format the file.

Seems like this is issue, we can have editor.formatOnSave and php-cs-fixer.fixOnSave enabled at all times.

Raised PR to fix this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.