From 11932e4b9c18177444786a97d8f30b1f7d876adb Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Thu, 16 May 2013 16:14:01 +0100 Subject: [PATCH 1/3] Fix for ignores in PHPCS --- PHPCI/Plugin/PhpCodeSniffer.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/PHPCI/Plugin/PhpCodeSniffer.php b/PHPCI/Plugin/PhpCodeSniffer.php index 21d0f2fe..c987fe76 100644 --- a/PHPCI/Plugin/PhpCodeSniffer.php +++ b/PHPCI/Plugin/PhpCodeSniffer.php @@ -33,12 +33,6 @@ class PhpCodeSniffer implements \PHPCI\Plugin $ignore = ''; if (count($this->phpci->ignore)) { - $map = function ($item) { - return substr($item, -1) == '/' ? $item . '*' : $item . '/*'; - }; - - $ignore = array_map($map, $this->phpci->ignore); - $ignore = ' --ignore=' . implode(',', $ignore); } From 953a209d6d75250ba1f25ff6481a471d9c4ce609 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Thu, 16 May 2013 16:25:26 +0100 Subject: [PATCH 2/3] Fixing exclude functionality in PHPMD --- PHPCI/Plugin/PhpMessDetector.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/PHPCI/Plugin/PhpMessDetector.php b/PHPCI/Plugin/PhpMessDetector.php index a2e7795b..b03f4459 100644 --- a/PHPCI/Plugin/PhpMessDetector.php +++ b/PHPCI/Plugin/PhpMessDetector.php @@ -29,11 +29,6 @@ class PhpMessDetector implements \PHPCI\Plugin $ignore = ''; if (count($this->phpci->ignore)) { - $map = function ($item) { - return substr($item, -1) == '/' ? $item . '*' : $item . '/*'; - }; - $ignore = array_map($map, $this->phpci->ignore); - $ignore = ' --exclude ' . implode(',', $ignore); } From 7d9abf21fbece721dd68e1251eb61f87043430a7 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Thu, 16 May 2013 16:46:30 +0100 Subject: [PATCH 3/3] Fixing PHPMD and PHPCS plugins, updating code to pass both, and updating phpci.yml to enable them. Issue #18 --- PHPCI/Application.php | 9 +++++ PHPCI/Builder.php | 4 +- PHPCI/Command/GenerateCommand.php | 2 +- PHPCI/Command/InstallCommand.php | 8 ++-- PHPCI/Controller/ProjectController.php | 55 ++++++++++++++------------ PHPCI/Controller/UserController.php | 10 ++--- PHPCI/Plugin/Mysql.php | 6 +-- PHPCI/Plugin/PhpCodeSniffer.php | 4 +- PHPCI/Plugin/PhpMessDetector.php | 2 +- PHPCI/Plugin/PhpUnit.php | 20 ++++++++-- phpci.yml | 13 ++++-- 11 files changed, 82 insertions(+), 51 deletions(-) diff --git a/PHPCI/Application.php b/PHPCI/Application.php index 47f13217..a284c7c5 100644 --- a/PHPCI/Application.php +++ b/PHPCI/Application.php @@ -18,9 +18,14 @@ use b8\Registry; */ class Application extends b8\Application { + /** + * Handle an incoming web request. + */ public function handleRequest() { $controllerName = \b8\Registry::getInstance()->get('ControllerName'); + + // Validate the user's session unless it is a login/logout action or a web hook: $sessionAction = ($controllerName == 'Session' && in_array($this->action, array('login', 'logout'))); $webhookAction = in_array($controllerName, array('Bitbucket', 'Github')); @@ -28,12 +33,16 @@ class Application extends b8\Application $this->validateSession(); } + // Render content into layout and return: $view = new b8\View('Layout'); $view->content = parent::handleRequest(); return $view->render(); } + /** + * Validate whether or not the remote user has a valid session: + */ protected function validateSession() { if (!empty($_SESSION['user_id'])) { diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index da0efbfc..421288c2 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -191,7 +191,7 @@ class Builder if (is_array($message)) { foreach ($message as $item) { if (is_callable($this->logCallback)) { - call_user_func_array($this->logCallback, $prefix . $item); + call_user_func_array($this->logCallback, array($prefix . $item)); } $this->log .= $prefix . $item . PHP_EOL; @@ -201,7 +201,7 @@ class Builder $this->log .= $message . PHP_EOL; if (isset($this->logCallback) && is_callable($this->logCallback)) { - call_user_func_array($this->logCallback, $prefix . $item); + call_user_func_array($this->logCallback, array($message)); } } diff --git a/PHPCI/Command/GenerateCommand.php b/PHPCI/Command/GenerateCommand.php index 57bb209c..c74d0632 100644 --- a/PHPCI/Command/GenerateCommand.php +++ b/PHPCI/Command/GenerateCommand.php @@ -30,7 +30,7 @@ class GenerateCommand extends Command ->setDescription('Generate models and stores from the database.'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute() { $gen = new \b8\Database\CodeGenerator(\b8\Database::getConnection(), 'PHPCI', PHPCI_DIR . '/PHPCI/'); $gen->generateModels(); diff --git a/PHPCI/Command/InstallCommand.php b/PHPCI/Command/InstallCommand.php index f28c0b8f..93cfe25a 100644 --- a/PHPCI/Command/InstallCommand.php +++ b/PHPCI/Command/InstallCommand.php @@ -32,7 +32,7 @@ class InstallCommand extends Command ->setDescription('Install PHPCI.'); } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute() { $dbHost = $this->ask('Enter your MySQL host: '); $dbName = $this->ask('Enter the database name PHPCI should use: '); @@ -101,9 +101,9 @@ b8\Database::setReadServers(array('{$dbHost}')); print $question . ' '; $rtn = ''; - $fp = fopen('php://stdin', 'r'); - $rtn = fgets($fp); - fclose($fp); + $stdin = fopen('php://stdin', 'r'); + $rtn = fgets($stdin); + fclose($stdin); $rtn = trim($rtn); diff --git a/PHPCI/Controller/ProjectController.php b/PHPCI/Controller/ProjectController.php index 7585bbea..54c6e28c 100644 --- a/PHPCI/Controller/ProjectController.php +++ b/PHPCI/Controller/ProjectController.php @@ -58,13 +58,13 @@ class ProjectController extends b8\Controller header('Location: /build/view/' . $build->getId()); } - public function delete($id) + public function delete($projectId) { if (!Registry::getInstance()->get('user')->getIsAdmin()) { throw new \Exception('You do not have permission to do that.'); } - $project = $this->_projectStore->getById($id); + $project = $this->_projectStore->getById($projectId); $this->_projectStore->delete($project); header('Location: /'); @@ -109,16 +109,16 @@ class ProjectController extends b8\Controller $tempPath = getenv("SystemRoot") . '/TEMP/'; } - $id = $tempPath . md5(microtime(true)); + $keyFile = $tempPath . md5(microtime(true)); if (!is_dir($tempPath)) { mkdir($tempPath); } - shell_exec('ssh-keygen -q -t rsa -b 2048 -f '.$id.' -N "" -C "deploy@phpci"'); + shell_exec('ssh-keygen -q -t rsa -b 2048 -f '.$keyFile.' -N "" -C "deploy@phpci"'); - $pub = file_get_contents($id . '.pub'); - $prv = file_get_contents($id); + $pub = file_get_contents($keyFile . '.pub'); + $prv = file_get_contents($keyFile); $values = array('key' => $prv, 'pubkey' => $pub, 'token' => $_SESSION['github_token']); } @@ -126,22 +126,7 @@ class ProjectController extends b8\Controller $form = $this->projectForm($values); if ($method != 'POST' || ($method == 'POST' && !$form->validate())) { - $gh = \b8\Registry::getInstance()->get('github_app'); - $code = $this->getParam('code', null); - - if (!is_null($code)) { - $http = new \b8\HttpClient(); - $url = 'https://github.com/login/oauth/access_token'; - $params = array('client_id' => $gh['id'], 'client_secret' => $gh['secret'], 'code' => $code); - $resp = $http->post($url, $params); - - if ($resp['success']) { - parse_str($resp['body'], $resp); - $_SESSION['github_token'] = $resp['access_token']; - header('Location: /project/add'); - die; - } - } + $this->handleGithubResponse(); $view = new b8\View('ProjectForm'); $view->type = 'add'; @@ -165,14 +150,34 @@ class ProjectController extends b8\Controller die; } - public function edit($id) + protected function handleGithubResponse() + { + $github = \b8\Registry::getInstance()->get('github_app'); + $code = $this->getParam('code', null); + + if (!is_null($code)) { + $http = new \b8\HttpClient(); + $url = 'https://github.com/login/oauth/access_token'; + $params = array('client_id' => $github['id'], 'client_secret' => $github['secret'], 'code' => $code); + $resp = $http->post($url, $params); + + if ($resp['success']) { + parse_str($resp['body'], $resp); + $_SESSION['github_token'] = $resp['access_token']; + header('Location: /project/add'); + die; + } + } + } + + public function edit($projectId) { if (!Registry::getInstance()->get('user')->getIsAdmin()) { throw new \Exception('You do not have permission to do that.'); } $method = Registry::getInstance()->get('requestMethod'); - $project = $this->_projectStore->getById($id); + $project = $this->_projectStore->getById($projectId); if ($method == 'POST') { $values = $this->getParams(); @@ -181,7 +186,7 @@ class ProjectController extends b8\Controller $values['key'] = $values['git_key']; } - $form = $this->projectForm($values, 'edit/' . $id); + $form = $this->projectForm($values, 'edit/' . $projectId); if ($method != 'POST' || ($method == 'POST' && !$form->validate())) { $view = new b8\View('ProjectForm'); diff --git a/PHPCI/Controller/UserController.php b/PHPCI/Controller/UserController.php index ed93891c..39857aab 100644 --- a/PHPCI/Controller/UserController.php +++ b/PHPCI/Controller/UserController.php @@ -74,14 +74,14 @@ class UserController extends b8\Controller die; } - public function edit($id) + public function edit($userId) { if (!Registry::getInstance()->get('user')->getIsAdmin()) { throw new \Exception('You do not have permission to do that.'); } $method = Registry::getInstance()->get('requestMethod'); - $user = $this->_userStore->getById($id); + $user = $this->_userStore->getById($userId); if ($method == 'POST') { $values = $this->getParams(); @@ -90,7 +90,7 @@ class UserController extends b8\Controller $values['admin'] = $values['is_admin']; } - $form = $this->userForm($values, 'edit/' . $id); + $form = $this->userForm($values, 'edit/' . $userId); if ($method != 'POST' || ($method == 'POST' && !$form->validate())) { $view = new b8\View('UserForm'); @@ -155,13 +155,13 @@ class UserController extends b8\Controller return $form; } - public function delete($id) + public function delete($userId) { if (!Registry::getInstance()->get('user')->getIsAdmin()) { throw new \Exception('You do not have permission to do that.'); } - $user = $this->_userStore->getById($id); + $user = $this->_userStore->getById($userId); $this->_userStore->delete($user); header('Location: /user'); diff --git a/PHPCI/Plugin/Mysql.php b/PHPCI/Plugin/Mysql.php index f813a8d6..500dc922 100644 --- a/PHPCI/Plugin/Mysql.php +++ b/PHPCI/Plugin/Mysql.php @@ -31,10 +31,10 @@ class Mysql implements \PHPCI\Plugin $this->phpci = $phpci; $this->queries = $options; - $db = \b8\Database::getConnection('write')->getDetails(); + $config = \b8\Database::getConnection('write')->getDetails(); $this->host = PHPCI_DB_HOST; - $this->user = $db['user']; - $this->pass = $db['pass']; + $this->user = $config['user']; + $this->pass = $config['pass']; $buildSettings = $phpci->getConfig('build_settings'); if (isset($buildSettings['mysql'])) { diff --git a/PHPCI/Plugin/PhpCodeSniffer.php b/PHPCI/Plugin/PhpCodeSniffer.php index c987fe76..13d915c8 100644 --- a/PHPCI/Plugin/PhpCodeSniffer.php +++ b/PHPCI/Plugin/PhpCodeSniffer.php @@ -31,9 +31,9 @@ class PhpCodeSniffer implements \PHPCI\Plugin public function execute() { $ignore = ''; - + if (count($this->phpci->ignore)) { - $ignore = ' --ignore=' . implode(',', $ignore); + $ignore = ' --ignore=' . implode(',', $this->phpci->ignore); } $cmd = PHPCI_BIN_DIR . 'phpcs --standard=%s %s "%s"'; diff --git a/PHPCI/Plugin/PhpMessDetector.php b/PHPCI/Plugin/PhpMessDetector.php index b03f4459..42c59b08 100644 --- a/PHPCI/Plugin/PhpMessDetector.php +++ b/PHPCI/Plugin/PhpMessDetector.php @@ -29,7 +29,7 @@ class PhpMessDetector implements \PHPCI\Plugin $ignore = ''; if (count($this->phpci->ignore)) { - $ignore = ' --exclude ' . implode(',', $ignore); + $ignore = ' --exclude ' . implode(',', $this->phpci->ignore); } $cmd = PHPCI_BIN_DIR . 'phpmd "%s" text codesize,unusedcode,naming %s'; diff --git a/PHPCI/Plugin/PhpUnit.php b/PHPCI/Plugin/PhpUnit.php index 9c1808a7..71954839 100644 --- a/PHPCI/Plugin/PhpUnit.php +++ b/PHPCI/Plugin/PhpUnit.php @@ -38,10 +38,22 @@ class PhpUnit implements \PHPCI\Plugin public function __construct(\PHPCI\Builder $phpci, array $options = array()) { $this->phpci = $phpci; - $this->directory = isset($options['directory']) ? $options['directory'] : null; - $this->xmlConfigFile = isset($options['config']) ? $options['config'] : null; - $this->runFrom = isset($options['run_from']) ? $options['run_from'] : null; - $this->args = isset($options['args']) ? $options['args'] : ''; + + if(isset($options['directory'])) { + $this->directory = $options['directory']; + } + + if(isset($options['config'])) { + $this->xmlConfigFile = $options['config']; + } + + if(isset($options['run_from'])) { + $this->runFrom = $options['run_from']; + } + + if(isset($options['args'])) { + $this->args = $options['args']; + } } public function execute() diff --git a/phpci.yml b/phpci.yml index d70e867b..6bd1a211 100644 --- a/phpci.yml +++ b/phpci.yml @@ -2,11 +2,16 @@ build_settings: verbose: false ignore: - "vendor" + - "assets" + - "build" + - "Tests" + - "composer.phar" setup: - composer: - action: "install" + composer: + action: "install" test: - php_mess_detector: - allow_failures: true \ No newline at end of file + php_mess_detector: + php_code_sniffer: + standard: "PSR2" \ No newline at end of file