Configure CodeSniffer on PhpStorm using Docker - php

I am developing a project using PhpStorm and I am using Php 7.1 with Docker. I would like to integrate PHP code sniffer in PhpStorm.
In PhpStorm I go to Settings|Languages&Frameworks|PHP|CodeSniffer and I try to add a new configuration, I provide as PHP Code Sniffer (phpcs) path the path of a script with the following content:
#!/usr/bin/env bash
docker run --rm -ti --volume "$(pwd):/app:rw" -u $(id -u):$(id -g) prooph/php:7.1-cli php vendor/bin/phpcs "$#"
which just runs phpcs in my docker container.
When I click on Validate, PhpStorm gives me the following message:
The input device is not a TTY
The same script works perfectly when it's run from PhpStorm console.
Am I doing something wrong? Or what I am trying to do is just not supported?

You should run it without -t flag:
#!/usr/bin/env bash
docker run --rm -i --volume "$(pwd):/app:rw" -u $(id -u):$(id -g) prooph/php:7.1-cli php vendor/bin/phpcs "$#"
-t allocates a pseudo-TTY, that's why it works on PhpStorm console.

In PhpStorm 2019 it works out of the box, without the need of a bash script wrapper.
First, you need to go to Project Settings > Languages & Frameworks > PHP.
Click the button ... next to the field "CLI interpreter" and configure it to works with Docker, selecting your Image name.
Once you have configured the CLI interpreter, go to Project Settings > Languages & Frameworks > PHP > Quality Tools and click on the three dots (...) button next to PHP_CodeSniffer. Add new configuration that uses the CLI interpreter you just added and fill the CodeSniffer path (in my case this is "/opt/project/vendor/bin/phpcs").
VoilĂ !

Related

Docker PHP application start

I'm trying to run a ready PHP app (a copy of existing site - just want to run it locally) in Docker.
I tried some from that: https://semaphoreci.com/community/tutorials/dockerizing-a-php-application but no luck. One friend told me that I should also have a server for that which confused me a bit. I know PHP have self-hosting capabilities (not recommended for production tho, but I need it just for testing purposes).
I also tried docker run -it --rm --name my-running-script -p 8080:8080 -v /Users/jans/Developer/sfolder:/usr/src/myapp -w /usr/src/myapp php:5.6 bash and run it from bash with php index.php (and yes - I used ls earlier to see if there are any files in there and they are), but nothing happens and localhost:8080 is not available.
I'm also trying to use https://hub.docker.com/r/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5/ but this is really complicated to me and can't do anything at all with it.
Anyone with experience how to run PHP app will be more than welcome.

Debug Symfony CLI application with PhpStorm inside Docker container

My current setup involve PhpStorm IDE in which I have imported Symfony 3 projects which is basically CLI tool. On the host machine I don't have PHP installed so I'm running the application from Docker container which has PHP and Xdebug installed.
I don't have issues to debug web applications from Docker containers but with Symfony and this CLI tool it seems a little bit more tricky.
My question is how to properly set this up and debug it from PhpStorm? I tried to create a new debug configuration (PHP Remote Debug) but breakpoints are not trigged.
Suppossing you have followed into the instructions mentioned into the following links:
Can't connect PhpStorm with xdebug with Docker
How to setup Docker + PhpStorm + xdebug on Ubuntu 16.04
Or similar questions
Then you need to follow theese steps:
Step1:
Get shell access to your container via running:
docker exec -ti ^container_id^ /bin/sh
Or if running a debian/ubuntu based one (or you installed manually bash):
docker exec -ti ^container_id^ /bin/bash
The ^container_id^ can be found via docker ps command it is the first column of the table. If running on a small window just pipe int into less -S resulting the command:
docker ps | less -S
Then export the following enviromental variables:
export PHP_IDE_CONFIG="serverName=0.0.0.0:5092"
export XDEBUG_CONFIG="idekey=PHPSTORM"
Please keep in mind to setup the correct value specified into Servers section as you see in the image:
It is important in order not to run into the problem specified in this question.
Then just enable debugger listentin into the phpstorm and spawn the cli as you do when you run a symfony application.

PhpStorm doesn't read PHP configuration from Docker container

What I want
Run PHPUnit by terminal and configuration in PhpStorm
What I Have
Previous problem
Resolved in HERE - about not finding files
File that running
#!/usr/bin/env bash
# echo "Current working directory: '"$(pwd)"'"
cd $(pwd) && docker run --rm -t -v $(pwd):/var/www -e SYMFONY_ENV=dev ezsystems/php:7.1-v1-dev php $#
Summary
When I run this by terminal:
docker-phpez vendor/bin/phpunit --coverage-text
Everything working correctly.
When I try to run this with configuration of PhpStorm, I get warning that: PHP is not installed.
But running this as remote PHP interpreter, gives me:
docker://ezsystems/php:7.1-v1/php /opt/.phpstorm_helpers/phpunit.php --configuration /var/www/phpunit.xml.dist
Testing started at 17:06 ...
The value $_SERVER['IDE_PHPUNIT_PHPUNIT_PHAR'] is specified, but file doesn't exist '/var/www/vendor/bin/phpunit'
Process finished with exit code 1
Looking like mounting doesn't work.
Question:
do you know why? How to fix that?
In previous version of PhpStorm I didn't had any problem with that.
Right now, I have 2016.3.2.
Because after update you have to update tags for phpstorm_helpers docker image. There i put more how it should be fixed: https://youtrack.jetbrains.com/issue/IDEA-189164
After updating PHPStorm to 2017.1.4, what I did:
switched to docker configuration
phpunit loaded from composer's autoload.php
Started to working. Still don't know why docker-phpez not working by PHPStorm, but I will stop for now.

Unable to add command line tool support for the composer in Phpstorm

I'm using PHPStorm on Windows 10, WAMP server for php.exe and I've installed composer using ComposerSetup.exe for windows. I'm trying to add command line tool support for the composer in Phpstorm, but it fails and gives this error.
Problem
Failed to parse output as XML: Error on line 2: Content is not allowed in prolog.
Command
php.exe C:\ProgramData\ComposerSetup\bin\composer list --xml
Output
dir=$(d=$(dirname "$0"); cd "$d" && pwd)
# see if we are running in cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
# cygwin paths start with /cygdrive/ which will break windows PHP,
# so we need to translate the dir path to windows format. However
# we could be using cygwin PHP which does not require this, so we
# test if the path to PHP starts with /cygdrive/ rather than /usr/bin.
if [[ $(which php) == /cygdrive/* ]]; then
dir=$(cygpath -m $dir);
fi
fi
dir=$(echo $dir | sed 's/ /\ /g')
php "${dir}/composer.phar" $*
PHPStorm should know about the locally installed PHP, and all it needs is the composer.phar file that contains the PHP source code of Composer.
What you have given PHPStorm looks like a Windows batch file that detects some aspects of being called in Windows shells (like CMD vs. Cygwin), and THEN calls PHP with the path of composer.phar. This is no PHP source code that you could directly give to PHP, like PHPStorm does.
Only configure the path of the phar file in PHPStorm, not that batch file.
Alternatively, you can simply add Composer via the PHPStorm GUI - it should ask you to provide Composer if it isn't known, and a download possibility should be offered.
There is an extensive help text available at their website just by googling "phpstorm install composer": https://www.jetbrains.com/phpstorm/help/using-composer-dependency-manager.html

Laravel Homestead Installation: bash init.sh not working

I'm trying to install Laravel Homestead in order to be able to test my Laravel projects locally.
I'm following this tutorial: http://laravel.com/docs/4.2/homestead
In the "Installing Homestead" part I followed the second step "Manually Via Git (No Local PHP)",I don't have any PHP installed locally and can't use Composer. (Wasn't PHP supposed to be installed as part of Homestead?)
I got to this part:
Once you have installed the Homestead CLI tool, run the bash init.sh command to create the Homestead.yaml configuration file:
bash init.sh
In command prompt I tried to enter the following command:
bash init.sh
I got the following error:
'bash' is not recognized as an internal or external command, operable program or batch file.
Then I tried to enter the same command in Git Bash, and I got the following error:
bash: init.sh: No such file or directory
When I tried to enter in Git Bash simply
init.sh
I got the following error:
sh.exe": init.sh: command not found
I ran into this problem well. You need to hop into your terminal (I am on Mac.)
Type into your console: cd ~/Homestead
and to make sure you are in your newly generated Homestead folder (you can always click Finder > your home directory > Homestead to confirm it's where it should be),
then while in terminal simply type ls -- if it lists all of the files like Vagrantfile, composer.lock and bash init.sh, you should be in.
Once you know you are in the correct Homestead folder, it is now at that point in which you run bash init.sh
After doing this it now says Homestead initialized!
.sh is a unix/linux script executable, that isn't recognized on Windows. There's a init.bat included in the package, but it isn't referenced in the tutorial.
Just run init.bat from the command line. It will copy the files to:
%HOMEDRIVE%%HOMEPATH%\.homestead
You need install Git Bash, and then run commands into Git Bash.
Try init.bat that worked for me
If the init.bat refused to work
Go to the Homestead folder.
C:/Users/Computer Name/Homestead
You will see init.bat: double click to run it.
Problem solved.
I found the answer you may be looking for. I was having the same problems with this. Just make sure at the end of bash init.sh you put a ;
So in the git bash cmd you will have this line
$ bash init.sh;
Just do it using sudo.
$sudo bash init.sh

Categories