2013-01-31 20 views
5

Tôi đang sử dụng các móc nối trước SVN để xác thực tiêu chuẩn mã của tôi (PSR2) trước khi có thể cam kết. Điều này hoạt động hoàn hảo chỉ với một ngoại lệ. Các tệp kiểm thử đơn vị (PHPUnit) của tôi tồn tại trong lớp bootstrap của tôi hiện có của tất cả các hàm kiểm tra đơn vị tĩnh, nhưng cũng cho phép các thông báo lỗi trên định nghĩa lớp khởi động.Cho phép SVN cam kết với các cảnh báo móc PreCommit hiện có

Chuẩn PSR2 sẽ đưa ra cảnh báo khi cố gắng thực hiện điều này, vì bạn không thể có mã không có trong lớp hoặc chức năng trong tệp chứa định nghĩa lớp.

Có ai có cách nào để loại trừ lỗi này trong mã số của tôi hoặc cách để làm cho mã của tôi hợp lệ (không đặt mã để kích hoạt thông báo lỗi của tôi trong mỗi hàm tĩnh của lớp bootstrap)?

Dưới đây là các file:

<?php 
namespace AlbumTest; 

use Zend\Loader\AutoloaderFactory; 
use Zend\Mvc\Service\ServiceManagerConfig; 
use Zend\ServiceManager\ServiceManager; 
use Zend\Stdlib\ArrayUtils; 
use RuntimeException; 

error_reporting(E_ALL | E_STRICT); 
chdir(__DIR__); 

class Bootstrap 
{ 
    protected static $serviceManager; 
    protected static $config; 
    protected static $bootstrap; 

    public static function init() 
    { 
     // Load the user-defined test configuration file, if it exists; otherwise, load 
     if (is_readable(__DIR__ . '/TestConfig.php')) { 
      $testConfig = include __DIR__ . '/TestConfig.php'; 
     } else { 
      $testConfig = include __DIR__ . '/TestConfig.php.dist'; 
     } 

     $zf2ModulePaths = array(); 

     if (isset($testConfig['module_listener_options']['module_paths'])) { 
      $modulePaths = $testConfig['module_listener_options']['module_paths']; 
      foreach ($modulePaths as $modulePath) { 
       if (($path = static::findParentPath($modulePath))) { 
        $zf2ModulePaths[] = $path; 
       } 
      } 
     } 

     $zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR; 
     $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') 
                  ? ZF2_MODULES_TEST_PATHS : ''); 

     static::initAutoloader(); 

     // use ModuleManager to load this module and it's dependencies 
     $baseConfig = array(
      'module_listener_options' => array(
       'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths), 
      ), 
     ); 

     $config = ArrayUtils::merge($baseConfig, $testConfig); 

     $serviceManager = new ServiceManager(new ServiceManagerConfig()); 
     $serviceManager->setService('ApplicationConfig', $config); 
     $serviceManager->get('ModuleManager')->loadModules(); 

     static::$serviceManager = $serviceManager; 
     static::$config = $config; 
    } 

    public static function getServiceManager() 
    { 
     return static::$serviceManager; 
    } 

    public static function getConfig() 
    { 
     return static::$config; 
    } 

    protected static function initAutoloader() 
    { 
     $vendorPath = static::findParentPath('vendor'); 

     if (is_readable($vendorPath . '/autoload.php')) { 
      $loader = include $vendorPath . '/autoload.php'; 
     } else { 
      $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') 
              ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') 
              ? $vendorPath . '/ZF2/library' : false)); 

      if (!$zf2Path) { 
       throw new RuntimeException(
        'Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.' 
       ); 
      } 

      include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; 

     } 

     AutoloaderFactory::factory(
      array(
       'Zend\Loader\StandardAutoloader' => array(
        'autoregister_zf' => true, 
        'namespaces' => array(
         __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__, 
        ), 
       ), 
      ) 
     ); 
    } 

    protected static function findParentPath($path) 
    { 
     $dir = __DIR__; 
     $previousDir = '.'; 
     while (!is_dir($dir . '/' . $path)) { 
      $dir = dirname($dir); 
      if ($previousDir === $dir) { 
       return false; 
      } 
      $previousDir = $dir; 
     } 
     return $dir . '/' . $path; 
    } 
} 

Bootstrap::init(); 

Trả lời

0

Làm thế nào để bạn precommit-móc như thế nào? Có thể bạn chỉ cần loại bỏ một vài dòng đầu tiên trước khi gửi đến Codesniffer? Một giải pháp khác là đặt error_reporting trong init của bootstrap của bạn

+0

Hook trước cam kết của tôi chỉ cần kiểm tra mã của tôi cho các quy tắc PSR-2 khi cam kết. Loại bỏ các dòng sẽ là lạ, nhưng hãy giải thích về ý tưởng này ... Đưa báo cáo lỗi trong init của tôi sẽ làm việc cho chức năng đó, nhưng tôi sẽ phải sao chép nó vào tất cả các chức năng khác. Và đó không phải là giải pháp mà tôi đang tìm kiếm. – Sander

+0

Mã Sniffer của bạn lấy dữ liệu như thế nào? Có thể đọc từ stdin, vì vậy loại bỏ các tập tin đầu tiên có thể được thực hiện trên bay, mà không tạo ra các tập tin tạm thời? –

Các vấn đề liên quan