Skip to content

Commit

Permalink
Fixes #1725. Improve PHP-CS-Fixer support with handling script path
Browse files Browse the repository at this point in the history
Detect if the executable path is either .phar (PHP) or not and
run the executable PHP-CS-Fixer differently considering.
  • Loading branch information
Glavin001 committed Jun 19, 2017
1 parent eafc43f commit 8b5363f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 48 deletions.
14 changes: 8 additions & 6 deletions examples/simple-jsbeautifyrc/php/expected/test.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
<?php

$hello = 'world';
$hello = "world";

if (!isset($_SESSION)) {
session_start();
}
require_once 'sql.req.php';
require_once("sql.req.php");


// friend requests
$q = $mysqli->query('');
$q = $mysqli->query("");
$num = $q->num_rows;
echo '<a href="notifications.php">';
if ($num > 0) {
if ($num>0) {
echo '<i class="fa fa-star white"></i>';
} else {
echo '<i class="fa fa-star-o"></i>';
}
echo '</a>';


// new messages
$q = $mysqli->query('');
$q = $mysqli->query("");
$num = $q->num_rows;
echo '<a href="messages.php">';
if ($num > 0) {
if ($num>0) {
echo '<i class="fa fa-envelope white"></i>';
} else {
echo '<i class="fa fa-envelope-o"></i>';
Expand Down
24 changes: 15 additions & 9 deletions src/beautifiers/executable.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ class Executable
@isVersion(@versionsSupported)

isVersion: (range) ->
semver.satisfies(@version, range)
@versionSatisfies(@version, range)

versionSatisfies: (version, range) ->
semver.satisfies(version, range)

getConfig: () ->
atom?.config.get("#{parentConfigKey}.#{@key}") or {}
Expand All @@ -129,21 +132,16 @@ class Executable
###
run: (args, options = {}) ->
@debug("Run: ", @cmd, args, options)
{ cwd, ignoreReturnCode, help, onStdin, returnStderr, returnStdoutOrStderr } = options
exeName = @cmd
config = @getConfig()
{ cmd, cwd, ignoreReturnCode, help, onStdin, returnStderr, returnStdoutOrStderr } = options
exeName = cmd or @cmd
cwd ?= os.tmpDir()

# Resolve executable and all args
Promise.all([@shellEnv(), this.resolveArgs(args)])
.then(([env, args]) =>
@debug('exeName, args:', exeName, args)

# Get PATH and other environment variables
if config and config.path
exePath = config.path
else
exePath = @which(exeName)
exePath = @path(exeName)
Promise.all([exeName, args, env, exePath])
)
.then(([exeName, args, env, exePath]) =>
Expand Down Expand Up @@ -198,6 +196,14 @@ class Executable
)
)

path: (cmd = @cmd) ->
config = @getConfig()
if config and config.path
Promise.resolve(config.path)
else
exeName = cmd
@which(exeName)

resolveArgs: (args) ->
args = _.flatten(args)
Promise.all(args)
Expand Down
63 changes: 30 additions & 33 deletions src/beautifiers/php-cs-fixer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ module.exports = class PHPCSFixer extends Beautifier
cmd: "php-cs-fixer"
homepage: "https://github.com/FriendsOfPHP/PHP-CS-Fixer"
installation: "https://github.com/FriendsOfPHP/PHP-CS-Fixer#installation"
optional: true
version: {
parse: (text) -> text.match(/version (.*) by/)[1] + ".0"
parse: (text) ->
try
text.match(/version (.*) by/)[1] + ".0"
catch
text.match(/PHP CS Fixer (\d+\.\d+\.\d+)/)[1]
}
docker: {
image: "unibeautify/php-cs-fixer"
Expand Down Expand Up @@ -66,7 +71,10 @@ module.exports = class PHPCSFixer extends Beautifier
"--allow-risky=#{options.allow_risky}" if options.allow_risky
"--using-cache=no"
]
if phpCsFixer.isVersion('1.x')

isVersion1 = ((phpCsFixer.isInstalled and phpCsFixer.isVersion('1.x')) or \
(options.cs_fixer_version and phpCsFixer.versionSatisfies("#{options.cs_fixer_version}.0.0", '1.x')))
if isVersion1
phpCsFixerOptions = [
"fix"
"--level=#{options.level}" if options.level
Expand All @@ -82,43 +90,32 @@ module.exports = class PHPCSFixer extends Beautifier

# Find php-cs-fixer.phar script
if options.cs_fixer_path
@deprecate("The \"cs_fixer_path\" has been deprecated. Please switch to using the config with path \"Executables - PHP-CS-Fixer - Path\" in Atom-Beautify package settings now.")
deprecationMessage = "The \"PHP - PHP-CS-Fixer Path (cs_fixer_path)\" configuration option has been deprecated. Please switch to using the option named \"Executables - PHP-CS-Fixer - Path\" in Atom-Beautify package settings now."
@deprecate(deprecationMessage)

@Promise.all([
@which(options.cs_fixer_path) if options.cs_fixer_path
@which('php-cs-fixer')
phpCsFixer.path()
tempFile = @tempFile("temp", text, '.php')
]).then(([customPath, phpCsFixerPath]) =>
paths = [customPath, phpCsFixerPath]
@debug('php-cs-fixer paths', paths)
_ = require 'lodash'
]).then(([customPhpCsFixerPath, phpCsFixerPath]) =>
# Get first valid, absolute path
phpCSFixerPath = _.find(paths, (p) -> p and path.isAbsolute(p) )
@verbose('phpCSFixerPath', phpCSFixerPath)
@debug('phpCSFixerPath', phpCSFixerPath, paths)
finalPhpCsFixerPath = if customPhpCsFixerPath and path.isAbsolute(customPhpCsFixerPath) then \
customPhpCsFixerPath else phpCsFixerPath
@verbose('finalPhpCsFixerPath', finalPhpCsFixerPath, phpCsFixerPath, customPhpCsFixerPath)

isPhpScript = (finalPhpCsFixerPath.indexOf(".phar") isnt -1) or (finalPhpCsFixerPath.indexOf(".php") isnt -1)
@verbose('isPhpScript', isPhpScript)

# Check if PHP-CS-Fixer path was found
if phpCSFixerPath?
# Found PHP-CS-Fixer path
if @isWindows
php.run([phpCSFixerPath, phpCsFixerOptions, tempFile], runOptions)
.then(=>
@readFile(tempFile)
)
else
@run(phpCSFixerPath, [phpCsFixerOptions, tempFile], runOptions)
.then(=>
@readFile(tempFile)
)
if finalPhpCsFixerPath and isPhpScript
php.run([finalPhpCsFixerPath, phpCsFixerOptions, tempFile], runOptions)
.then(=>
@readFile(tempFile)
)
else
@verbose('php-cs-fixer not found!')
# Could not find PHP-CS-Fixer path
@Promise.reject(@commandNotFoundError(
'php-cs-fixer'
{
link: "https://github.com/FriendsOfPHP/PHP-CS-Fixer"
program: "php-cs-fixer.phar"
pathOption: "PHP - CS Fixer Path"
})
phpCsFixer.run([phpCsFixerOptions, tempFile],
Object.assign({}, runOptions, { cmd: finalPhpCsFixerPath })
)
.then(=>
@readFile(tempFile)
)
)

0 comments on commit 8b5363f

Please sign in to comment.