Last active
          August 29, 2015 14:07 
        
      - 
      
 - 
        
Save tommy-muehle/d1bc9b9a5115da4c0a67 to your computer and use it in GitHub Desktop.  
    How to add a coding-standard to phpcs via composer hook
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| namespace TM; | |
| use Composer\Package\Package; | |
| use Composer\Script\Event; | |
| /** | |
| * Class MyClass | |
| * | |
| * @package TM | |
| */ | |
| class MyClass | |
| { | |
| /** | |
| * @var array | |
| */ | |
| protected static $requiredPackage = array( | |
| 'name' => 'squizlabs/php_codesniffer', | |
| 'version' => '1.5.4' | |
| ); | |
| /** | |
| * @param Event $event | |
| */ | |
| public static function postUpdate(Event $event) | |
| { | |
| static::addStandard($event); | |
| } | |
| /** | |
| * @param Event $event | |
| */ | |
| public static function postInstall(Event $event) | |
| { | |
| static::addStandard($event); | |
| } | |
| /** | |
| * @param Event $event | |
| */ | |
| protected static function addStandard(Event $event) | |
| { | |
| $composer = $event->getComposer(); | |
| /* @var $repositoryManager \Composer\Repository\RepositoryManager */ | |
| $repositoryManager = $composer->getRepositoryManager(); | |
| $requiredPackage = $repositoryManager->findPackage( | |
| self::$requiredPackage['name'], self::$requiredPackage['version'] | |
| ); | |
| if (!$requiredPackage instanceof Package) { | |
| $event->getIO()->write(sprintf('<error>Required package "%s" does not exist!</error>', self::$requiredPackage['name'])); | |
| return; | |
| } | |
| $vendorDir = $composer->getConfig()->get('vendor-dir'); | |
| $path = $vendorDir . '/squizlabs/php_codesniffer/CodeSniffer/Standards/Symfony2'; | |
| if (is_dir($path)) { | |
| $event->getIO()->write('<info>Symfony2 coding standard installed.</info>'); | |
| return; | |
| } | |
| /* @var $downloadManager \Composer\Downloader\DownloadManager */ | |
| $downloadManager = $composer->getDownloadManager(); | |
| /* @var $downloader \Composer\Downloader\ZipDownloader */ | |
| $downloader = $downloadManager->getDownloader('zip'); | |
| $package = new Package('lapistano-Symfony2-coding-standard', '1.0', '1.0'); | |
| $package->setDistUrl('https://github.com/lapistano/Symfony2-coding-standard/archive/master.zip'); | |
| $downloader->download($package, $path); | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | { | |
| "require-dev": { | |
| "squizlabs/php_codesniffer": "1.5.4" | |
| }, | |
| "scripts": { | |
| "post-update-cmd": "TM\\MyClass::postUpdate", | |
| "post-install-cmd": "TM\\MyClass::postInstall" | |
| }, | |
| ... | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment