Esempio n. 1
0
 public function __construct()
 {
     // don't read config file, if any
     XRef::setConfigFileName("default");
     XRef::setConfigValue("git.repository-dir", ".");
     $this->xref = new XRef();
 }
Esempio n. 2
0
 public function __construct()
 {
     // don't read config file, if any
     XRef::setConfigFileName("default");
     XRef::setConfigValue("xref.data-dir", "tmp");
     $this->xref = new XRef();
 }
Esempio n. 3
0
 public function resetPlugins()
 {
     $this->xref->resetPlugins();
     $this->xref->loadPluginGroup('lint');
     XRef::setConfigValue("lint.check-global-scope", true);
 }
Esempio n. 4
0
}
if (XRef::needHelp()) {
    $program = basename($argv[0]);
    XRef::showHelpScreen("xref-lint - tool to find problems in PHP source code", "{$program} [options] [files to check]");
    exit(1);
}
if (isset($options['init'])) {
    $xref = new XRef();
    $xref->init();
    exit(0);
}
//
// report-level:  errors, warnings or notices
// Option -r <value> is a shortcut for option -d lint.report-level=<value>
if (isset($options['report-level'])) {
    XRef::setConfigValue("lint.report-level", $options['report-level']);
}
//
// output-format: text or json
//
$outputFormat = 'text';
if (isset($options['output'])) {
    $outputFormat = $options['output'];
}
if ($outputFormat != 'text' && $outputFormat != 'json') {
    die("unknown output format: {$outputFormat}");
}
// git mode:
if (isset($options['git-cached']) && $options['git-cached'] || isset($options['git-rev']) && $options['git-rev']) {
    $options['git'] = true;
}
Esempio n. 5
0
    public function testCI()
    {
        $old_rev = "377d2edc1da549a80b5b44286e7dcaf59cee300a";
        $current_rev = "190fc6a9fddcae0313decbbbce92e4a83bf47ab9";
        XRef::setConfigValue('mail.reply-to', '*****@*****.**');
        XRef::setConfigValue('mail.from', '*****@*****.**');
        XRef::setConfigValue('mail.to', array('*****@*****.**', '{%ae}', '{%an}@xref-lint.net'));
        XRef::setConfigValue('project.name', 'unit-test');
        XRef::setConfigValue('project.source-url', 'https://github.com/gariev/xref/blob/{%revision}/{%fileName}#L{%lineNumber}');
        XRef::setConfigValue('xref.smarty-class', self::SMARTY_CLASS_PATH);
        XRef::setConfigValue('xref.data-dir', 'tmp');
        $scm = $this->xref->getSourceCodeManager();
        $file_provider_old = $scm->getFileProvider($old_rev);
        $file_provider_new = $scm->getFileProvider($current_rev);
        $modified_files = $scm->getListOfModifiedFiles($old_rev, $current_rev);
        $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
        $errors = $lint_engine->getIncrementalReport($file_provider_old, $file_provider_new, $modified_files);
        list($recipients, $subject, $body, $headers) = $this->xref->getNotificationEmail($errors, 'tests-git', $old_rev, $current_rev);
        //print_r(array($recipients, $subject, $body, $headers));
        // assert that our comparison function works
        $this->assertTrue($this->strSmartSpaces("\t hello,\r \n world  ") == $this->strSmartSpaces("hello, world"));
        $this->assertTrue($this->strSmartSpaces("\t hello,\r \n world  ") != $this->strSmartSpaces("hello , world"));
        $this->assertTrue(count($recipients) == 3);
        $this->assertTrue($recipients[0] == '*****@*****.**');
        $this->assertTrue($recipients[1] == '*****@*****.**');
        $this->assertTrue($recipients[2] == '*****@*****.**');
        $this->assertTrue($this->strSmartSpaces($subject) == "XRef CI unit-test: tests-git/190fc6a");
        $expected_body = <<<END
            <html><body>
            Hi, you've got this e-mail as the author (gariev) of commit 190fc6a to branch tests-git.
            It looks like there are problems in file(s) modified since previous revision 377d2ed:
                <ul>
                    <li>broken.php</li>
                    <ul>
                        <li>
                            <span class='error'>error</span>
                            (<a href="https://github.com/gariev/xref/blob/master/README.md#xr010">xr010</a>): Use of unknown variable (\$error)
                             at <a href="https://github.com/gariev/xref/blob/190fc6a9fddcae0313decbbbce92e4a83bf47ab9/broken.php#L3">line 3</a>
                        </li>
                    </ul>
                </ul>
            <p>If the problems above are real, you can fix them.
            If these warnings are from code merged from other branch and not result of merge conflict, you can ignore them or find the author of the original commit.
            If the report is wrong, you can help <a href='mailto:gariev@hotmail.com?subject=xref'>improve XRef CI</a> itself and/or ignore this e-mail.
            </p>
            <p><small>
              Generated by XRef CI version <<VERSION>>. About XRef:
                <a href="https://github.com/gariev/xref/blob/master/README.md">documentation</a>,
                <a href="https://github.com/gariev/xref/">source code</a>,
                <a href="http://xref-lint.net/bin/xref-lint.php">online tool</a>.
            </small></p>
            </body></html>
END;
        $expected_body = str_replace('<<VERSION>>', XRef::version(), $expected_body);
        $this->assertTrue($this->strSmartSpaces($body) == $this->strSmartSpaces($expected_body));
        $expected_headers = <<<END
            MIME-Version: 1.0
            Content-type: text/html
            Reply-to: no-reply@xref-lint.net
            From: ci-server@xref-lint.net
END;
        $this->assertTrue($this->strSmartSpaces($headers) == $this->strSmartSpaces($expected_headers));
    }
Esempio n. 6
0
    public function testConfigConstants()
    {
        XRef::setConfigValue('lint.add-constant', array('foo', 'FooBar', 'BAZ'));
        $testPhpCode = '<?php

            class Foo {
                const BAZ = 10;
            }

            echo foo;       //ok
            echo FooBar;    // ok
            echo Foo::BAZ;  // ok
            echo BAZ;       // ok, global BAZ
            echo unknown;   // warning
        ';
        $exceptedDefects = array(array('unknown', 11, XRef::WARNING));
        $this->checkPhpCode($testPhpCode, $exceptedDefects);
        XRef::setConfigValue('lint.add-constant', array());
    }
Esempio n. 7
0
    public function testVariablesAssignedByFunctions()
    {
        // function that can assign values to variables passed by reference:
        // list of known functions both that can and cannot set variable's value
        //
        //  strict mode:
        //      known_function_that_assign_variable($unknown_var);          // ok
        //      known_function_that_doesnt_assign_variable($unknown_var);   // error
        //      unknown_function($unknown_var);                             // warning
        //      unknown_function($unknown_var_in_expression*2);             // error
        // relaxed mode:
        //      known_function_that_assign_variable($unknown_var);          // ok
        //      known_function_that_doesnt_assign_variable($unknown_var);   // warning
        //      unknown_function($unknown_var);                             // warning
        //      unknown_function($unknown_var_in_expression*2);             // warning
        //
        // list-of-known-function =
        //      explicit list of functions +
        //      config file defined funcions +
        //      result of get_defined_functions() +     // don't overwrite functions from above
        //      parsing of current file                 // overwrite or not?
        //
        $testPhpCode = '<?php

            function foo () {
                // internal functions
                preg_match("#pattern#", "string-to-be-mateched", $matches);     // ok
                preg_match("#pattern#", "string-to-be-mateched", null);         // error (non-var pass by ref)
                preg_grep("pattern", $input);                                   // error
                sort($array);                                                   // error
                sort( array(1,2,3) );                                           // error (non-var pass by ref)

                // locally-defined functions
                local_function_with_pass_by_reference_argument2(1, $var2);      // ok
                local_function_with_pass_by_reference_argument2($var3, $var4);  // error in $var3 only

                // unknown functions
                unknown_function($unknown_var);                                 // warning
                unknown_function($unknown_var_in_expression*2);                 // error
            }

            function bar ($args) {
                extract($args);                                                 // relaxed mode from here

                // internal functions
                preg_match("#pattern#", "string-to-be-mateched", $matches);     // ok
                preg_match("#pattern#", "string-to-be-mateched", null);         // error (non-var pass by ref)
                preg_grep("pattern", $input);                                   // warning
                sort($array);                                                   // warning
                sort( array(1,2,3) );                                           // error (non-var pass by ref)

                // locally-defined functions
                local_function_with_pass_by_reference_argument2(1, $var2);      // ok
                local_function_with_pass_by_reference_argument2($var3, $var4);  // warning in $var3 only

                // unknown functions
                unknown_function($unknown_var);                                 // warning
                unknown_function($unknown_var_in_expression*2);                 // warning
            }

            function local_function_with_pass_by_reference_argument2($arg1, &$arg2) {
                $arg2 = $arg1;
            }

            sort( array(1,2,3) );                                               // error (non-var pass by ref)
            sort( Foo::$bar );                                                  // ok

        ';
        $expectedDefects = array(array('null', 6, XRef::ERROR), array('$input', 7, XRef::ERROR), array('$array', 8, XRef::ERROR), array('array', 9, XRef::ERROR), array('$var3', 13, XRef::ERROR), array('$unknown_var', 16, XRef::WARNING), array('$unknown_var_in_expression', 17, XRef::ERROR), array('null', 25, XRef::ERROR), array('$input', 26, XRef::WARNING), array('$array', 27, XRef::WARNING), array('array', 28, XRef::ERROR), array('$var3', 32, XRef::WARNING), array('$unknown_var', 35, XRef::WARNING), array('$unknown_var_in_expression', 36, XRef::WARNING), array('array', 43, XRef::ERROR));
        $this->checkPhpCode($testPhpCode, $expectedDefects);
        $testPhpCode = '<?php

            class Foo {
                public function preg_match() {}
                public function sort(&$x)    {}

                public function bar() {
                    $this->preg_match("", "", $x);      // error: method preg_match doesnt initialize vars
                    Foo::preg_match("", "", $y);        // error
                    self::preg_match("", "", $z);       // error
                    preg_match("", "", $ok);            // ok, this is internal preg_match

                    $this->sort($a);                    // ok
                    Foo::sort($b);                      // ok
                    self::sort($c);                     // ok
                    sort($d);                           // error - internal sort doesnt intialize vars
                }
            }

            function test () {
                Foo::preg_match("", "", $i);                // error
                preg_match("", "", $j);                     // ok
                $foo = new SomeClass();
                $foo->preg_match("", "", $k);               // warning: unknown preg_match (?)

                Foo::sort($l);                              // ok
                sort($m);                                   // error, internal sort
                $foo->sort($n);                             // warning, unknown $foo sort
            }

            Foo::preg_match("", "", $i);                // warning (global relaxed scope, otherwise - error)
            preg_match("", "", $j);                     // ok
            $foo = new SomeClass();
            $foo->preg_match("", "", $k);               // warning: this is unknown preg_match

            Foo::sort($l);                              // ok
            sort($m);                                   // warning, internal sort
            $foo->sort($n);                             // warning, unknown sort
        ';
        $expectedDefects = array(array('$x', 8, XRef::ERROR), array('$y', 9, XRef::ERROR), array('$z', 10, XRef::ERROR), array('$d', 16, XRef::ERROR), array('$i', 21, XRef::ERROR), array('$k', 24, XRef::WARNING), array('$m', 27, XRef::ERROR), array('$n', 28, XRef::WARNING), array('$i', 31, XRef::WARNING), array('$k', 34, XRef::WARNING), array('$m', 37, XRef::WARNING), array('$n', 38, XRef::WARNING));
        $this->checkPhpCode($testPhpCode, $expectedDefects);
        //
        $testPhpCode = '<?php

            $arraysort = array();
            array_multisort($arraysort, SORT_ASC);  // ok, no error on second arg
            echo $expectedError;
        ';
        $expectedDefects = array(array('$expectedError', 5, XRef::WARNING));
        $this->checkPhpCode($testPhpCode, $expectedDefects);
        //
        XRef::setConfigValue('lint.add-function-signature', array('foo(&$x)', 'Foo::bar($a, &$b)', 'Bar::baz(&$a, &$b)', '?::qux(&$a, &$b)'));
        // re-create the lint plugins with new config settings
        $this->resetPlugins();
        $testPhpCode = '<?php

            function t() {
                foo($x);                    // ok
                foo($y, $z);                // error on $z

                Foo::bar(1, $a);            // ok
                Foo::bar($b, $c);           // error on $b

                $i = 0;
                $x->baz($i, $j);            // warning on $j
                $l = $m = 10;
                $x->foo->baz($k, $l, $m);   // warning on $k

                $x->qux($p, $q);            // ok, unknown class of $x, matches "?::qux"
                $p->foo->bar->qux($r);      // ok
                $x->qux($s, $t, $u);        // error on $u

            }
        ';
        $expectedDefects = array(array('$z', 5, XRef::ERROR), array('$b', 8, XRef::ERROR), array('$j', 11, XRef::WARNING), array('$k', 13, XRef::WARNING), array('$u', 17, XRef::ERROR));
        $this->checkPhpCode($testPhpCode, $expectedDefects);
        $testPhpCode = '<?php

            class Foo {
                public $bar = array();
                public static $baz = array();
            }

            function passed_by_ref_arg(Array &$a) {
                echo array_pop($a);
            }

            $a = array();
            $foo = new Foo;

            passed_by_ref_arg($a);                  // ok
            passed_by_ref_arg($foo->bar);           // ok
            passed_by_ref_arg(Foo::$baz);           // ok
            passed_by_ref_arg(\\External\\Code::$x);  // ok
            passed_by_ref_arg(Some\\Other::$z);      // ok
            passed_by_ref_arg( array(1, 2, 3) );    // error
        ';
        $expectedDefects = array(array('array', 20, XRef::ERROR));
        $this->checkPhpCode($testPhpCode, $expectedDefects);
    }