diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-01-28 22:08:50 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-02-10 11:51:24 +0100 |
commit | d74662df7df72ad9ec238b78223acc0e7f65311f (patch) | |
tree | ffadb56b1bb0c0dfa11cb54a10a67186aff31306 /lib/private/app/codechecker.php | |
parent | 5ae03fd650b6f3665d1c69ead674d4f5d6420513 (diff) | |
download | nextcloud-server-d74662df7df72ad9ec238b78223acc0e7f65311f.tar.gz nextcloud-server-d74662df7df72ad9ec238b78223acc0e7f65311f.zip |
implement php code checker to detect usage of not allowed private APIs - including console command to check local code to be used by developers
Diffstat (limited to 'lib/private/app/codechecker.php')
-rw-r--r-- | lib/private/app/codechecker.php | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/lib/private/app/codechecker.php b/lib/private/app/codechecker.php new file mode 100644 index 00000000000..28816a8fdc5 --- /dev/null +++ b/lib/private/app/codechecker.php @@ -0,0 +1,115 @@ +<?php +/** + * Copyright (c) 2015 Thomas Müller <deepdiver@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\App; + +use OC\Hooks\BasicEmitter; +use PhpParser\Lexer; +use PhpParser\Node; +use PhpParser\Node\Name; +use PhpParser\NodeTraverser; +use PhpParser\NodeVisitorAbstract; +use PhpParser\Parser; +use RecursiveCallbackFilterIterator; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; +use RegexIterator; +use SplFileInfo; + +class CodeChecker extends BasicEmitter { + + const CLASS_EXTENDS_NOT_ALLOWED = 1000; + const CLASS_IMPLEMENTS_NOT_ALLOWED = 1001; + const STATIC_CALL_NOT_ALLOWED = 1002; + const CLASS_CONST_FETCH_NOT_ALLOWED = 1003; + const CLASS_NEW_FETCH_NOT_ALLOWED = 1004; + + public function __construct() { + $this->parser = new Parser(new Lexer); + $this->blackListedClassNames = [ + // classes replaced by the public api + 'OC_API', + 'OC_App', + 'OC_AppConfig', + 'OC_Avatar', + 'OC_BackgroundJob', + 'OC_Config', + 'OC_DB', + 'OC_Files', + 'OC_Helper', + 'OC_Hook', + 'OC_Image', + 'OC_JSON', + 'OC_L10N', + 'OC_Log', + 'OC_Mail', + 'OC_Preferences', + 'OC_Request', + 'OC_Response', + 'OC_Template', + 'OC_User', + 'OC_Util', + ]; + } + + /** + * @param string $appId + * @return array + */ + public function analyse($appId) { + $appPath = \OC_App::getAppPath($appId); + if ($appPath === false) { + throw new \RuntimeException("No app with given id <$appId> known."); + } + + $errors = []; + + $excludes = array_map(function($item) use ($appPath) { + return $appPath . '/' . $item; + }, ['vendor', '3rdparty', '.git', 'l10n']); + + $iterator = new RecursiveDirectoryIterator($appPath, RecursiveDirectoryIterator::SKIP_DOTS); + $iterator = new RecursiveCallbackFilterIterator($iterator, function($item) use ($appPath, $excludes){ + /** @var SplFileInfo $item */ + foreach($excludes as $exclude) { + if (substr($item->getPath(), 0, strlen($exclude)) === $exclude) { + return false; + } + } + return true; + }); + $iterator = new RecursiveIteratorIterator($iterator); + $iterator = new RegexIterator($iterator, '/^.+\.php$/i'); + + foreach ($iterator as $file) { + /** @var SplFileInfo $file */ + $this->emit('CodeChecker', 'analyseFileBegin', [$file->getPathname()]); + $errors = array_merge($this->analyseFile($file), $errors); + $this->emit('CodeChecker', 'analyseFileFinished', [$errors]); + } + + return $errors; + } + + /** + * @param string $file + * @return array + */ + public function analyseFile($file) { + $code = file_get_contents($file); + $statements = $this->parser->parse($code); + + $visitor = new CodeCheckVisitor($this->blackListedClassNames); + $traverser = new NodeTraverser; + $traverser->addVisitor($visitor); + + $traverser->traverse($statements); + + return $visitor->errors; + } +} |