Skip to content

Commit

Permalink
Merge pull request #11 from mansoorkhan96/feat/support-workspace-conf…
Browse files Browse the repository at this point in the history
…ig-file

Feat/support workspace config file
  • Loading branch information
mansoorkhan96 authored Nov 1, 2022
2 parents c7cdcd4 + 4dfeb1f commit e7302a2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ All notable changes to the "php-cs-fixer" extension will be documented in this f
## 0.0.8

- Update `php-cs-fixer.phar` to v3.12.0

## 1.0.0

- Support `WorkspaceFolder/.php-cs-fixer.php` OR `WorkspaceFolder/php-cs-fixer.dist.php`
- Update `php-cs-fixer.phar` to v3.13.0
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@
<img width="150px" height="150px" src="icon.png"/>
</div>

<div align="center"><h1>PHP CS Fixer (php-cs-fixer)</h1></div>
<div align="center"><h1>PHP CS Fixer (Prettier for PHP)</h1></div>

PHP CS Fixer (Prettier for PHP) extension for PHP developers. This extension requires almost zero configuration to format `.php` files. It uses `v3.12.0` of [cs.symfony.com](https://cs.symfony.com/) by default but that's easily replaceable.
This extension requires almost zero configuration to format `.php` files. It uses `v3.13.0` of [cs.symfony.com](https://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](simple-demo.gif)

## Getting Started

As long as PHP 7+ is installed on your system and in your PATH, the extension should work out of the box.

## 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: false)
* `php-cs-fixer.allowRisky`: Determines whether risky rules are allowed (--allow-risky) (default: false)
* `php-cs-fixer.config`: Path to a .php-cs-fixer.php file (--config) (default: "")
* `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)

### Need to use a custom `php-cs-fixer.phar` file?

The extension uses `v3.12.0` of [cs.symfony.com](https://cs.symfony.com/) by default but you can easily override it with a new or old file. Download the required file version from above link and provide the file path to the extension.
[Download](https://cs.symfony.com/) the required file version from above link and provide the file path to the extension.

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

Expand All @@ -39,9 +35,11 @@ On Windows:
"php-cs-fixer.toolPath": "C:\\Users\\username\\.vscode\\php-cs-fixer.phar",
```

### Need to use a custom `.php-cs-fixer.php` rules file?
### 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.

PHP CS Fixer formats `.php` files using a set of rules defined in a file. The extension uses `PSR12` as defualt rule set. If you need to change this behavour, simply create a custom `.php-cs-fixer.php` config file and add rules of your choice. After creating the file you need to provide file path to the extension.
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:

Expand All @@ -57,7 +55,7 @@ On Windows:

#### Not sure which rules to add?

Try out my `.php-cs-fixer.php` config file that I use in my projects. Try to remove a rule and observe the changes in file formatting.
Try out following config rules. Try to remove a rule and observe the changes.

```php
<?php
Expand Down Expand Up @@ -100,4 +98,4 @@ return $config
->setLineEnding("\n");
```

Find complete rule set [here](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/ruleSets/index.rst)
You can find the complete rule set [here](https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/ruleSets/index.rst)
22 changes: 20 additions & 2 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function formatDocument(document) {
}

args.push('--config=' + config);
} else if (projectConfigFile()) {
args.push('--config=' + projectConfigFile());
} else {
let rules = getConfig('rules');
if (rules) {
Expand All @@ -66,8 +68,6 @@ function formatDocument(document) {
const tmpFile = tmp.fileSync();
fs.writeFileSync(tmpFile.name, document.getText(null));

// console.log('php-cs-fixer temporary file: ' + tmpFile.name);

return new Promise(function (resolve) {
cp.execFile('php', [...args, tmpFile.name], opts, function (err) {
if (err) {
Expand All @@ -90,6 +90,24 @@ function formatDocument(document) {
});
}

function getProjectRoot() {
return vscode.workspace.getWorkspaceFolder(
vscode.window.activeTextEditor.document.uri
).uri.fsPath;
}

function projectConfigFile() {
if (fs.existsSync(getProjectRoot() + path.sep + '.php-cs-fixer.php')) {
return getProjectRoot() + path.sep + '.php-cs-fixer.php'
}

if (fs.existsSync(getProjectRoot() + path.sep + 'php-cs-fixer.dist.php')) {
return getProjectRoot() + path.sep + 'php-cs-fixer.dist.php'
}

return null
}

function registerDocumentProvider(document, options) {
return new Promise(function (resolve, reject) {
formatDocument(document).then(function (text) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "PHP CS Fixer",
"description": "PHP CS Fixer extension for VS Code with zero config",
"icon": "icon.png",
"version": "0.0.8",
"version": "1.0.0",
"publisher": "mansoorkhan96",
"engines": {
"vscode": "^1.18.0"
Expand Down
Binary file modified php-cs-fixer
Binary file not shown.

0 comments on commit e7302a2

Please sign in to comment.