Code Monkey home page Code Monkey logo

tuli's Introduction

Build Status

A static analysis engine...

Usage:

bin/tuli analyze file1 file2 path

Installation

Install it as a composer dependency!!!

$ composer require ircmaxell/tuli dev-master

Then simply execute vendor/bin/tuli as normal

Or check it out into its own project. Then composer install the dependencies:

$ composer install

Then simply bin/tuli to execute.

Example:

code.php:

<?php

$a = 1.0;
$b = 2;

$c = foo($a, $b);

$d = foo($b, $c);

function foo(int $a, int $b): int {
    if ($a > $b) {
        return $a + $b + 0.5;
    }
}

Then, in shell:

$ bin/tuli analyze code.php
Analyzing code.php
Determining Variable Types
Round 1 (15 unresolved variables out of 20)
.
Detecting Type Conversion Issues
Type mismatch on foo() argument 0, found float expecting int code.php:6
Type mismatch on foo() return value, found float expecting int code.php:12
Default return found for non-null type int code.php:10
Done

The three errors it found are:

  • Type mismatch on foo() argument 0, found float expecting int code.php:6

    Meaning that at code.php on line 6, you're passing a float to the first argument when it declared an integer

  • Type mismatch on foo() return value, found float expecting int code.php:12

    The value that's being returned on line 12 is a float, but it was declared as an integer in the function signature.

  • Default return found for non-null type int code.php:10

    There's a default return statement (not supplied) for a typed function

That's it!

Currently Supported Rules:

  • Function Argument Types

    It will check all typed function arguments and determine if all calls to that function match the type.

  • Function Return Types

    If the function's return value is typed, it will determine if the function actually returns that type.

  • Method Argument Types

    It will check all calls to a method for every valid typehint permutation to determine if there's a possible mismatch.

Todo:

  • A lot

Another example:

<?php

class A {
    public function foo(int $a) : int {
        return $a;
    }
}

class B extends A {
    public function foo(float $a) : float {
        return $a;
    }
}

class C extends B {
    public function foo(int $a) : int {
        return $a;
    }
}

function foo(A $a) : int {
    return $a->foo(1.0);
}

Running:

$ bin/tuli analyze code.php
Analyzing code.php

Determining Variable Types
Round 1 (5 unresolved variables out of 7)

Round 2 (3 unresolved variables out of 7)

Detecting Type Conversion Issues
Detecting Function Argument Errors
Detecting Function Return Errors
Type mismatch on foo() return value, found float expecting int code.php:22
Detecting Method Argument Errors
Type mismatch on A->foo() argument 0, found float expecting int code.php:22
Type mismatch on C->foo() argument 0, found float expecting int code.php:22
Done

Again, it found 3 errors:

  • Type mismatch on foo() return value, found float expecting int code.php:22

    It looked at all possible A::foo() method definitions (A::foo, B::foo, C::foo), and it detmermined that the general return type is float (since type widening allows int to be passed to float, but not the other way around). Therefore, returning ->foo() directly can result in a type error.

  • Type mismatch on A->foo() argument 0, found float expecting int code.php:22

  • Type mismatch on C->foo() argument 0, found float expecting int code.php:22

    We know that if you use type A or C, you're trying to pass a float to something that declares an integer.

tuli's People

Contributors

anlutro avatar ircmaxell avatar jyggen avatar kelunik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tuli's Issues

Fatal error related to assignment by reference

$ ./vendor/bin/tuli analyze classes/Utils/ArrayUtil.php
Analyzing classes/Utils/ArrayUtil.php
PHP Catchable fatal error:  Argument 1 passed to PHPCfg\Parser::parseExpr_AssignRef() must be an instance of PhpParser\Node\Expr\Assign, instance of PhpParser\Node\Expr\AssignRef given, called in /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php on line 656 and defined in /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php on line 707
PHP Stack trace:
PHP   1. {main}() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/tuli/bin/tuli:0
PHP   2. Cilex\Application->run() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/tuli/bin/tuli:26
PHP   3. Symfony\Component\Console\Application->run() /home/andreas/dev/php/autarky-framework/vendor/cilex/cilex/src/Cilex/Application.php:66
PHP   4. Symfony\Component\Console\Application->doRun() /home/andreas/dev/php/autarky-framework/vendor/symfony/console/Application.php:123
PHP   5. Symfony\Component\Console\Application->doRunCommand() /home/andreas/dev/php/autarky-framework/vendor/symfony/console/Application.php:192
PHP   6. Symfony\Component\Console\Command\Command->run() /home/andreas/dev/php/autarky-framework/vendor/symfony/console/Application.php:841
PHP   7. Tuli\Command\Analyze->execute() /home/andreas/dev/php/autarky-framework/vendor/symfony/console/Command/Command.php:259
PHP   8. Tuli\Command->execute() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/tuli/lib/Tuli/Command/Analyze.php:32
PHP   9. Tuli\Command->getGraphsFromFiles() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/tuli/lib/Tuli/Command.php:59
PHP  10. PHPCfg\Parser->parse() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/tuli/lib/Tuli/Command.php:107
PHP  11. PHPCfg\Parser->parseNodes() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:62
PHP  12. PHPCfg\Parser->parseNode() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:86
PHP  13. PHPCfg\Parser->parseStmt_Namespace() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:100
PHP  14. PHPCfg\Parser->parseNodes() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:382
PHP  15. PHPCfg\Parser->parseNode() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:86
PHP  16. PHPCfg\Parser->parseStmt_Class() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:100
PHP  17. PHPCfg\Parser->parseNodes() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:115
PHP  18. PHPCfg\Parser->parseNode() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:86
PHP  19. PHPCfg\Parser->parseStmt_ClassMethod() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:100
PHP  20. PHPCfg\Parser->parseNodes() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:144
PHP  21. PHPCfg\Parser->parseNode() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:86
PHP  22. PHPCfg\Parser->parseStmt_Foreach() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:100
PHP  23. PHPCfg\Parser->parseNodes() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:260
PHP  24. PHPCfg\Parser->parseNode() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:86
PHP  25. PHPCfg\Parser->parseExprNode() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:95
PHP  26. PHPCfg\Parser->parseExpr_AssignRef() /home/andreas/dev/php/autarky-framework/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:656

ArrayUtil.php source code

How it work under big project?

Hi I have web app under symfony framework

In this project I want to analyze my sources in src folder. And I don't want analyze any vendor files. How I can do it with tule?

Now then I run:

bin/tuli analyze ./src

It show me:

 Could not find parent symfony\component\httpkernel\bundle\bundle

And if I run it with some vendor it also fail with errors.

Math op on unknown types int + null|bool|int|float|string|object|array|callable

./bin/tuli print-cfg /usr/local/src/money/src/Money.php -vvv
Analyzing /usr/local/src/money/src/Money.php
Determining Variable Types
Round 1 (262 unresolved variables out of 447)
...............


  [RuntimeException]                                                               
  Math op on unknown types int + null|bool|int|float|string|object|array|callable  



Exception trace:
 () at /usr/local/src/Tuli/lib/Tuli/TypeReconstructor.php:225
 Tuli\TypeReconstructor->resolveVarOp() at /usr/local/src/Tuli/lib/Tuli/TypeReconstructor.php:111
 Tuli\TypeReconstructor->resolveVar() at /usr/local/src/Tuli/lib/Tuli/TypeReconstructor.php:58
 Tuli\TypeReconstructor->resolve() at /usr/local/src/Tuli/lib/Tuli/Command.php:70
 Tuli\Command->analyzeGraphs() at /usr/local/src/Tuli/lib/Tuli/Command.php:60
 Tuli\Command->execute() at /usr/local/src/Tuli/lib/Tuli/Command/PrintCFG.php:28
 Tuli\Command\PrintCFG->execute() at /usr/local/src/Tuli/vendor/symfony/console/Command/Command.php:259
 Symfony\Component\Console\Command\Command->run() at /usr/local/src/Tuli/vendor/symfony/console/Application.php:878
 Symfony\Component\Console\Application->doRunCommand() at /usr/local/src/Tuli/vendor/symfony/console/Application.php:195
 Symfony\Component\Console\Application->doRun() at /usr/local/src/Tuli/vendor/symfony/console/Application.php:126
 Symfony\Component\Console\Application->run() at /usr/local/src/Tuli/vendor/cilex/cilex/src/Cilex/Application.php:66
 Cilex\Application->run() at /usr/local/src/Tuli/bin/tuli:26


print-cfg [-x|--exclude EXCLUDE] [-i|--image IMAGE] [--] <files> (<files>)...

/usr/local/src/money is a clone of https://github.com/sebastianbergmann/money/ (master).

Tuli doesn't work with php 7.1

Tuli doesn't work with php 7.1

It does not recognise that CONTs can be now public/private
Syntax error, unexpected T_CONST, expecting T_FUNCTION on line 19

or not recognzse '?' null returns eg:
private function getContactField(int $id, int $type, string $field): ?string
give us error: Syntax error, unexpected '?' on line 434

Unknown type declaration found: *

Analyzing ../api/lib/Colourbox/Search/SearchRequestParameterMappings/RequestParameterMapper.php



  [RuntimeException]
  Unknown type declaration found: *



Exception trace:
 () at /var/www/cbx/Tuli/vendor/ircmaxell/php-cfg/lib/PHPCfg/AstVisitor/NameResolver.php:85
 PHPCfg\AstVisitor\NameResolver->parseTypeDecl() at /var/www/cbx/Tuli/vendor/ircmaxell/php-cfg/lib/PHPCfg/AstVisitor/NameResolver.php:53
 PHPCfg\AstVisitor\NameResolver->PHPCfg\AstVisitor\{closure}() at n/a:n/a
 preg_replace_callback() at /var/www/cbx/Tuli/vendor/ircmaxell/php-cfg/lib/PHPCfg/AstVisitor/NameResolver.php:56
 PHPCfg\AstVisitor\NameResolver->enterNode() at /var/www/cbx/Tuli/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:120
 PhpParser\NodeTraverser->traverseArray() at /var/www/cbx/Tuli/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:84
 PhpParser\NodeTraverser->traverseNode() at /var/www/cbx/Tuli/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:129
 PhpParser\NodeTraverser->traverseArray() at /var/www/cbx/Tuli/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:84
 PhpParser\NodeTraverser->traverseNode() at /var/www/cbx/Tuli/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:129
 PhpParser\NodeTraverser->traverseArray() at /var/www/cbx/Tuli/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:64
 PhpParser\NodeTraverser->traverse() at /var/www/cbx/Tuli/vendor/ircmaxell/php-cfg/lib/PHPCfg/Parser.php:60
 PHPCfg\Parser->parse() at /var/www/cbx/Tuli/lib/Tuli/Command.php:107
 Tuli\Command->getGraphsFromFiles() at /var/www/cbx/Tuli/lib/Tuli/Command.php:59
 Tuli\Command->execute() at /var/www/cbx/Tuli/lib/Tuli/Command/Analyze.php:32
 Tuli\Command\Analyze->execute() at /var/www/cbx/Tuli/vendor/symfony/console/Command/Command.php:259
 Symfony\Component\Console\Command\Command->run() at /var/www/cbx/Tuli/vendor/symfony/console/Application.php:878
 Symfony\Component\Console\Application->doRunCommand() at /var/www/cbx/Tuli/vendor/symfony/console/Application.php:195
 Symfony\Component\Console\Application->doRun() at /var/www/cbx/Tuli/vendor/symfony/console/Application.php:126
 Symfony\Component\Console\Application->run() at /var/www/cbx/Tuli/vendor/cilex/cilex/src/Cilex/Application.php:66
 Cilex\Application->run() at /var/www/cbx/Tuli/bin/tuli:26


analyze [-x|--exclude EXCLUDE] [--] <files> (<files>)...

logic exception should never happen

I cloned this git repo. Ran composer install for the dependencies. Then I ran bin/tuli analyze /path/to/project and this happened:

Could not find parent league\event\event
Could not find parent exception
Determining Variable Types
PHP Notice:  Trying to get property of non-object in /root/Tuli/vendor/ircmaxell/php-types/lib/PHPTypes/TypeReconstructor.php on line 31

  [LogicException]
  Should never happen

analyze [-x|--exclude EXCLUDE] [--] <files> (<files>)...

Error involving PHPTypes

I was just trying out Tuli, and as it was analyzing, it stopped with this error message:
PHP Catchable fatal error: Argument 2 passed to PHPTypes\State::findTypedBlock() must be an instance of PHPCfg\Block, null given, called inircmaxell/php-types/lib/PHPTypes/State.php on line 207and defined in ircmaxell/php-types/lib/PHPTypes/State.php on line 212

Any idea why $block would be null in that call?

Composer should not raise warnings.

MacBook-Pro-van-Tim:iRail-work tim$ sudo composer require ircmaxell/tuli dev-master
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- The requested package ircmaxell/tuli could not be found in any version, there may be a typo in the package name.

Potential causes:

Read https://getcomposer.org/doc/articles/troubleshooting.md for further common problems.

Installation failed, reverting ./composer.json to its original content.
MacBook-Pro-van-Tim:iRail-work tim$

Install

Add a section in the readme about the installation in projects.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.