コード例 #1
0
ファイル: SourceHandler.php プロジェクト: mfn/php-analyzer
 public static function addSourcesToProject(InputInterface $input, Project $project)
 {
     $inputSources = [];
     if (NULL !== $input->getOption('load-from')) {
         $inputSources = array_merge($inputSources, array_map('trim', file($input->getOption('load-from'))));
     }
     if (NULL !== $input->getArgument('sources')) {
         $inputSources = array_merge($inputSources, $input->getArgument('sources'));
     }
     $inputSources = array_unique($inputSources);
     if (empty($inputSources)) {
         throw new \RuntimeException('No input sources/directories for scanning provided.');
     }
     /** @var \SplFileInfo[] $files */
     $files = [];
     foreach ($inputSources as $inputSource) {
         if (is_dir($inputSource)) {
             $files = array_merge($files, Util::scanDir($inputSource));
         } else {
             if (is_file($inputSource)) {
                 $files[] = new \SplFileInfo($inputSource);
             } else {
                 throw new \RuntimeException("File {$inputSource} is not a file nor a directory.");
             }
         }
     }
     if (count($files) === 0) {
         throw new \RuntimeException('No source files to scan found');
     }
     foreach ($files as $file) {
         $project->addSplFileInfo($file);
     }
 }
コード例 #2
0
ファイル: AnalyzersTest.php プロジェクト: mfn/php-analyzer
 /**
  * Although this test seems redundant I use it to ensure that as far as it's
  * possible the analyzers do not negatively affect each other.
  */
 public function testAllAnalyzers()
 {
     $project = new Project(Null::getInstance());
     foreach (Util::scanDir(self::getAnalyzerTestsDir(), '/\\.phptest$/') as $file) {
         $project->addSplFileInfo(new \SplFileInfo($file));
     }
     $project->addAnalyzers(Project::getDefaultConfig());
     $project->analyze();
     $reports = $project->getAnalyzerReports();
     $this->assertSame(10, count($reports));
     $this->assertSame('Class Mfn\\PHP\\Analyzer\\Tests\\AbstractMethodMissing\\b misses the following abstract method: Mfn\\PHP\\Analyzer\\Tests\\AbstractMethodMissing\\a::b()', $reports[0]->getTimestampedReport()->getReport()->report());
     $this->assertSame('Class Mfn\\PHP\\Analyzer\\Tests\\InterfaceMethodMissing\\d misses the following interface method: Mfn\\PHP\\Analyzer\\Tests\\InterfaceMethodMissing\\a::c()', $reports[1]->getTimestampedReport()->getReport()->report());
     $this->assertSame('Declaration of Mfn\\PHP\\Analyzer\\Tests\\MethodDeclarationCompatibility\\b::c($a, $a) must be compatible with Mfn\\PHP\\Analyzer\\Tests\\MethodDeclarationCompatibility\\a::c(array $a = 1)', $reports[2]->getTimestampedReport()->getReport()->report());
     # Empty exception catch block reports
     $this->assertSame(27, $reports[3]->getSourceFragment()->getLineSegment()->getHighlightLine());
     $this->assertSame('Dynamic class instantiation with variable $foo in 003_dynamic_class_instantiation.phptest:26', $reports[4]->getTimestampedReport()->getReport()->report());
     $this->assertSame('Variable used in constructing raw SQL, is it escaped?', $reports[5]->getTimestampedReport()->getReport()->report());
     $this->assertSame(29, $reports[5]->getTimestampedReport()->getReport()->getSourceFragment()->getLineSegment()->getHighlightLine());
     $this->assertSame(30, $reports[6]->getTimestampedReport()->getReport()->getSourceFragment()->getLineSegment()->getHighlightLine());
     $this->assertSame(31, $reports[7]->getTimestampedReport()->getReport()->getSourceFragment()->getLineSegment()->getHighlightLine());
     # Empty exception catch block reports
     $this->assertSame(27, $reports[8]->getSourceFragment()->getLineSegment()->getHighlightLine());
     $this->assertSame(32, $reports[9]->getSourceFragment()->getLineSegment()->getHighlightLine());
 }
コード例 #3
0
 */
use Mfn\PHP\Analyzer\Analyzers\NameResolver;
use Mfn\PHP\Analyzer\Analyzers\ObjectGraph\Helper;
use Mfn\PHP\Analyzer\Analyzers\ObjectGraph\ObjectGraph;
use Mfn\PHP\Analyzer\Analyzers\Parser;
use Mfn\PHP\Analyzer\Logger\Stdout;
use Mfn\PHP\Analyzer\Project;
use Mfn\PHP\Analyzer\Util\Util;
use PhpParser\Lexer;
require_once __DIR__ . '/../vendor/autoload.php';
error_reporting(E_ALL);
Util::installMinimalError2ExceptionHandler();
$projectRealPath = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..');
# Use analyzer to gather the object graph
$project = new Project(new Stdout());
$project->addSplFileInfos(Util::scanDir(__DIR__ . '/../lib/'));
$project->addAnalyzers([new Parser(new \PhpParser\Parser(new Lexer())), new NameResolver(), $objectGraph = new ObjectGraph()]);
$project->analyze();
$helper = new Helper($objectGraph);
$className = 'Mfn\\PHP\\Analyzer\\Analyzers\\Analyzer';
$class = $objectGraph->getClassByFqn($className);
if (NULL === $class) {
    throw new \RuntimeException("Unable to find class {$className}");
}
unset($className);
$descendants = $helper->findExtends($class, true);
sort($descendants);
/** @var string[] $index */
$index = [];
$index[] = '# Built-in / available Analyzers';
$index[] = '';
コード例 #4
0
ファイル: bootstrap.php プロジェクト: mfn/php-analyzer
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Markus Fischer <*****@*****.**>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
use Mfn\PHP\Analyzer\Util\Util;
# Fall back to UTC if date.timezone is not set
$dateTimezone = ini_get('date.timezone');
if (empty($dateTimezone)) {
    date_default_timezone_set('UTC');
}
error_reporting(E_ALL);
Util::installMinimalError2ExceptionHandler();
コード例 #5
0
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
use Mfn\PHP\Analyzer\Util\Util;
/**
 * Reports files which don't have the license preamble.
 */
require_once __DIR__ . '/../vendor/autoload.php';
error_reporting(E_ALL);
Util::installMinimalError2ExceptionHandler();
$maxInspectNumLines = 5;
$directories = ['bin', 'lib', 'tests', 'tools'];
/** @var \SplFileInfo[] $files */
$files = [];
foreach ($directories as $directory) {
    $files = array_merge($files, Util::scanDir(__DIR__ . '/../' . $directory, '/\\.php(test)?$/'));
}
$first = true;
$foundMissing = false;
foreach ($files as $file) {
    $fp = fopen($file->getRealPath(), 'r');
    $inspectNumLines = $maxInspectNumLines;
    $found = false;
    while ($inspectNumLines-- && false !== ($line = fgets($fp))) {
        if (false !== strpos($line, '* The MIT License (MIT)')) {
            $found = true;
            break;
        }
    }
    if (!$found) {
        $foundMissing = true;