We should have hook_requirements that check if PHP CodeSniffer is installed.

We should have a drush command that makes installation easier.

See also #1763674: Script to configure a git repository to use this project

Comments

douggreen’s picture

Initial attempt http://drupalcode.org/project/coder.git/commit/fba2a4e

  1. This doesn't work on Windows, and we should either (a) display an error on windows, or (b) try to get it working right.
  2. Can we rely on pear? Is it everywhere?
  3. Can we automate the php.ini, or is that a step too far?
  4. drush_shell_exec for a symlink is a hack, I couldn't get php.net/symlink to work
benjifisher’s picture

I may have done something odd to the installation of pear on my Mac (OSX 10.6) but it requires "sudo" to install a package.

I am having trouble testing. Even after I uninstall PHP_CodeSniffer, the drush script tells me that it is already installed. Maybe I have another copy somewhere in my include path, or maybe the test in the drush script does not work.

Maybe add the --alldeps (or -a) option. When I install (not using the drush script) I get a message about the recommended package PHPTimer or something.

I think we should look for a way to install this without using pear.

benjifisher’s picture

I think the test for CodeSniffer.php does not work. I changed it to

  try {
    @include_once 'PHP/NoesNotExist.php';
    drush_print(dt('PHP_CodeSniffer already installed.'));
  }
  catch (Exception $e) {
    $missing = TRUE;
  }

and still got the message "PHP_CodeSniffer already installed."

benjifisher’s picture

StatusFileSize
new4.14 KB

The attached patch is a proof of concept. It downloads CodeSniffer from GitHub and unpacks it under ~/.drush/PHP. Despite the help text, it ignores the options. (That's what "proof of concept" means, right?) The code is based on some of my recent additions to the vimrc project.

Advantage: It does not rely on pear.

Disadvantage: It downloads bleeding-edge code with no idea what version it is.

To do: Implement the options. Modify php.ini and/or output a message to the user. Link Drupal sniffs to the Standards directory. Test on Windows.

Maybe we can combine the two scripts, giving the user the option to use pear or not.

$ drush help coder-install-codesniffer
Install PHP CodeSniffer (phpcs).

Examples:
 drush coder-install-codesniffer           Download PHP_CodeSniffer library to 
                                           $HOME/.drush/PHP/.                  
 sudo drush coder-install-codesniffer      Download to $HOME/.drush/PHP/,      
 --php-dir=/usr/lib/php --force="yes"      overwriting any existing            
                                           installation, and make a symbolic   
                                           link to /usr/lib/php/. The example  
                                           path is suitable for some versions  
                                           of Mac OSX.                         


Options:
 --force                                   Overwrite existing installation    
                                           ("yes" or "no", defaults to "no")  
 --install-dir                             Install directory (defaults to     
                                           $HOME/.drush/PHP/)                 
 --php-dir                                 Path to PHP include directory (may 
                                           require running under sudo)        


$ drush coder-install-codesniffer
$ php ~/.drush/PHP/PHP_CodeSniffer/scripts/phpcs -h
Usage: phpcs [-nwlsaepvi] [-d key[=value]]
    [--report=<report>] [--report-file=<reportfile>] [--report-<report>=<reportfile>] ...
benjifisher’s picture

Tested on Windows 7. Pear is not installed, and I do not feel like installing it. After applying the patch from #4 I got the same results as on my Mac.

I do not think it makes a difference, but I ran drush from a bash shell (the one installed along with git).

I even tried installing pear, following the instructions at http://pear.php.net/manual/en/installation.getting.php. I found the .bat file under Program Files (x86)\Drush\Php. I could not run it from bash, so I switched to the drush-enhanced Windows command line. The batch script seems to use relative paths, so I had to switch to Program Files (x86)\Drush\Php before I could get anywhere. I tried installing it system-wide (the default) and locally, but I did not try changing the defaults for any of the directories. I got error messages about the php executable and not being able to create directories.

Conclusion: IMHO, Window users need a non-pear alternative.

douggreen’s picture

http://drupalcode.org/project/coder.git/commit/9293399 should fix hook_requirements problem reported above in #3

benjifisher’s picture

Status: Needs review » Needs work

I updated my installation to the latest git HEAD. I still have the problem I described in #3. I uninstalled PHP_CodeSniffer and made the same test change as in #3, and I still get "PHP_CodeSniffer already installed."

The change in 9293399 affects coder_review_requirements(). I tried making the same change in drush_coder_review_install_php_code_sniffer() and then$ sudo drush install-php-code-sniffer
worked.

What do you think of my suggestion in #4?

mgifford’s picture

This sounds good to me. I was talking to someone recently who had a heck of a lot of trouble setting this up in a Acquia Dev desktop environment, opted to set it up in a virtual machine instead.

Using drush version 6.0-dev

The drush command 'install-php-code-sniffer' could not be found. Run `drush cache-clear drush` to clear the commandfile cache if you have installed new extensions. [error]
Drush was not able to start (bootstrap) the Drupal database. [error]
Hint: This may occur when Drush is trying to:
* bootstrap a site that has not been installed or does not have a configured database. In this case you can select another site with a working database setup by specifying the URI to use with
the --uri parameter on the command line. See `drush topic docs-aliases` for details.
* connect the database through a socket. The socket file may be wrong or the php-cli may have no access to it in a jailed shell. See http://drupal.org/node/1428638 for details.

I would like to do some more code reviews using this tool.

douggreen’s picture

@ benjifisher I don't like downloaded non-stable code. That's something that pear does for us, only gets the stable releases. I could see adding something as a fallback for when pear is not installed, ... but couldn't the fallback be something more like telling the user how to install pear, or are there too many systems that this is impossible for?

Is there another Drupal project that relies on third party plugins, and offers a drush command to download and install, that we might use as a model?

benjifisher’s picture

@douggreen:

Maybe there is a way to get a specific commit as a zip download from GitHub instead of the current HEAD.

See my comments in #5 (not just the first line). I tried pretty hard to install PEAR on W7, without success. If I can find some time, I may try the instructions at http://nikic.github.com/2011/12/03/Manually-installing-PEAR-on-Windows.html. It starts off,

So, you tried installing PEAR on Windows using their go-pear.phar installer and it - for some unknown reason - didn’t work?

This seems promising, since it suggests that the blogger had the same trouble I did.

alar’s picture

Sometimes it's reassuring to know that others are suffering too.
I'm able to install Wet Boew Drupal on Linux but WIndows 7 is causing me grief. it looks like pear is required however I've never had this sort of problem before. The last message before total failure is:

WD php: Warning: include_once(): Failed opening                        [warning]
'c:/Users/xyz/Sites/drupal_wet/' for inclusion
(include_path='.;C:\php\pear') in _drupal_theme_initialize() (line
208 of C:\Users\allison\Sites\drupal_wet\includes\theme.inc).

Then I read on http://pear.php.net :

Windows 7 will not allow the installation script to write to c:\windows\pear.ini. It gives the "Fatal error: Call to undefined method PEAR_Error::set()" error. Change the location of the configuration files (#11) to someplace writable, complete the installation, then copy the contents into c:\windows\pear.ini.

So I think this is another Windows 7 security feature. I'll look at the suggestions in #10. I wonder if I absolutely must have pear installed, maybe there is an easier fix???

benjifisher’s picture

@alar: I share your pain.

If you cannot get PEAR to work, you can try my drush script in #4 above.

albert volkman’s picture

Status: Needs work » Needs review
StatusFileSize
new1.25 KB

The help text says "Coder review uses PHP_CodeSniffer to perform better code review checks; Code_Sniffer is not a requirement, but it is helpful. Use "drush install-coder-sniffer" to help install." The command is actually "drush install-php-code-sniffer".

Yet Another User’s picture

StatusFileSize
new66.25 KB

Hi,

I have upgraded from Drupal 6 to 7. I found out that Coder module helps to convert the modules from 6 to 7.
I have installed the Coder module and when tried to run update.php. It said

"Coder Review PHP_CodeSniffer not installed.
Coder review uses PHP_CodeSniffer to perform better code review checks; Code_Sniffer is not a requirement, but it is helpful. Use "drush install-coder-sniffer" to help install."

I downloaded drush and installed it. Now when I tried "drush install-php-code-sniffer" command or any command, it gives the attached error. Please check the attachments.

I don't know much about drupal. It would be appreciated if anyone can help.

Thanks
Mohit Mittal

albert volkman’s picture

@Mohit Mittal It appears that you have some issues with your LAMP stack. Also, you'll need to execute this command from within your Drupal install.

benjifisher’s picture

If you are having trouble installing drush, first look through the drush README file. A lot of questions are answered there. If you still need help with drush, then the project page says where to look for support: https://drupal.org/project/drush .

If you want to get CodeSniffer installed before resolving your drush problems, see the instructions at https://drupal.org/node/1419988 . If you are using Windows, then some of the comments and links above may be helpful.

Yet Another User’s picture

StatusFileSize
new1.97 KB

Dear Albert and benjifisher,

Thanks for your replies.

I have applied the attached patch which somehow installed it. But when I try to run it, it still gives me

PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found
PHP/CodeSniffer.php not found

I tried to use coder_upgrade module for one of my modules, and it had converted it to make it compatible with Drupal 7.

kenorb’s picture

BTW. Where drush install-coder-sniffer command is defined?
As I've the error:

The drush command 'install-coder-sniffer' could not be found.
kenorb’s picture

matt v.’s picture

@kenorb,

See comment #13; the 7.x-2.0 version had a discrepancy between the name of the command in the help message versus the code. You may want to try the newer 7.x-2.1 release instead.

waverate’s picture

Using coder 7.x-2.2 and PHP_CodeSniffer 1.5.2 installed via PEAR.

Drupal Status page report:

Coder Review	PHP_CodeSniffer installed.
Coder review uses PHP_CodeSniffer to perform better code review checks; Code_Sniffer is not a requirement, but it is helpful. Use "drush install-php-code-sniffer" to help install.

I think the expected result would be to hide the comment if PHP_CodeSniffer installed and detected correctly.

This may be a trivial point to consider but the inconsistency of the spelling and capitalization of the words Coder Review and Code_Sniffer make it difficult to tell if there is a problem or if this is a good thing.

seworthi’s picture

coder_review.install:
In the function coder_review_requirements(), I changed the requirements array to this:

    $requirements['coder_review-sniffer'] = array(
      'title' => t('Coder Review'),
      'value' => empty($missing) ? $t('PHP_CodeSniffer installed.') : $t('PHP_CodeSniffer not installed.'),
      'description' => empty($missing) ? '' : $t('Coder review uses <a href="!php-url">PHP_CodeSniffer</a> to perform better code review checks; Code_Sniffer is not a requirement, but it is helpful. Use "<a href="!drush-url">drush</a> install-php-code-sniffer" to help install.', array('!php-url' => 'http://pear.php.net/package/PHP_CodeSniffer', '!drush-url' => 'http://drupal.org/project/drush')),
      'severity' => empty($missing) ? REQUIREMENT_OK : REQUIREMENT_WARNING,
    );
mile23’s picture

Version: 7.x-2.x-dev » 8.x-2.x-dev
Related issues: +#2744463: Add phpcs, coder 8.2.8 as --dev requirements for drupal/core

FYI: Working on adding phpcs as a --dev dependency of core: #2744463: Add phpcs, coder 8.2.8 as --dev requirements for drupal/core

klausi’s picture

Status: Needs review » Closed (outdated)

Coder is not a module anymore and the installation with composer is documented at https://www.drupal.org/node/1419988

Note that Coder 8.x-2.x can also be used to check Drupal 7 code.