From 319f6fe74e4d4d4db0067ffdfd15a99b67ba342f Mon Sep 17 00:00:00 2001 From: Simon Coggins Date: Thu, 31 Mar 2016 11:39:39 +1300 Subject: [PATCH] MDL-53663: Add support for static analysis via phan --- .phan/bin/mkfilelist | 10 ++++++ .phan/config.php | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++ composer.json | 3 +- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100755 .phan/bin/mkfilelist create mode 100644 .phan/config.php diff --git a/.phan/bin/mkfilelist b/.phan/bin/mkfilelist new file mode 100755 index 0000000..a7ab619 --- /dev/null +++ b/.phan/bin/mkfilelist @@ -0,0 +1,10 @@ +#!/bin/bash + +if [[ -z $WORKSPACE ]] +then + export WORKSPACE=. +fi +cd $WORKSPACE + +# you can exclude directories with -not -path "./excludedir/*" +find . -type f -name "*.php" diff --git a/.phan/config.php b/.phan/config.php new file mode 100644 index 0000000..2bb14ca --- /dev/null +++ b/.phan/config.php @@ -0,0 +1,95 @@ + true, + + // Allow null to be cast as any type and for any + // type to be cast to null. + "null_casts_as_any_type" => true, + + // Backwards Compatibility Checking + 'backward_compatibility_checks' => false, + + // Run a quick version of checks that takes less + // time + "quick_mode" => true, + + // Only emit critical issues + "minimum_severity" => 10, + + // A set of fully qualified class-names for which + // a call to parent::__construct() is required + 'parent_constructor_required' => [ + ], + + // A list of directories holding code that we want + // to parse, but not analyze + "exclude_analysis_directory_list" => get_exclude_list(), + +]; + + +/** + * Build a list of directories to parse but not analyse. + * This should include vendor code and third party libs. + * + * @return array Array of directory paths to exclude. + */ +function get_exclude_list() { + // Exclude vendor directory. + $excludelist = ['./vendor']; + + // Iterate the code directory and exclude any third party libs from the analysis + // (based on thirdpartylibs.xml files). + $wwwroot = Config::projectPath(''); + $Directory = new RecursiveDirectoryIterator($wwwroot, FilesystemIterator::SKIP_DOTS); + $Iterator = new RecursiveIteratorIterator($Directory); + $pregroot = preg_quote($wwwroot, '/'); + $Regex = new RegexIterator($Iterator, "/^({$pregroot}.*)\/thirdpartylibs\.xml$/i", RecursiveRegexIterator::GET_MATCH); + foreach ($Regex as $match) { + $filename = $match[0]; + $filepath = $match[1]; + $contents = file_get_contents($filename); + $xml = new SimpleXMLElement($contents); + foreach ($xml->library as $library) { + $excludelistitem = $filepath . DIRECTORY_SEPARATOR . $library->location; + if (strpos($excludelistitem, '*') !== false) { + // thirdpartylibs.xml supports wildcards - need to de-glob. + foreach (glob($excludelistitem) as $file) { + $excludelist[] = './' . substr($file, strlen($wwwroot)); + } + } else { + $excludelist[] = './' . substr($excludelistitem, strlen($wwwroot)); + } + } + } + return $excludelist; +} + diff --git a/composer.json b/composer.json index f578d7a..53b88f7 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "require-dev": { "phpunit/phpunit": "4.8.*", "phpunit/dbUnit": "1.4.*", - "moodlehq/behat-extension": "3.31.0" + "moodlehq/behat-extension": "3.31.0", + "etsy/phan": "dev-master" } } -- 2.7.4