Issues (19)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/lambda.php (4 issues)

Labels

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * This file is part of the Ariadne Component Library.
4
 *
5
 * (c) Muze <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace arc;
11
12 1
function singleton($f) {
13 2
    return function () use ($f) {
14 2
        static $result;
15 2
        if (null === $result) {
16 2
            if ( $f instanceof \Closure && isset($this) ) {
0 ignored issues
show
The variable $this seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
17
                $f = \Closure::bind($f, $this);
0 ignored issues
show
Consider using a different name than the imported variable $f, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
18
            }
19 2
            $result = $f();
20
        }
21 2
        return $result;
22 2
    };
23
}
24
25 1
function partial(callable $callable, $partialArgs, $defaultArgs=[] ) {
26 4
    $partialMerge = function($partialArgs, $addedArgs, $defaultArgs = [])
27
    {
28 4
        end( $partialArgs );
29 4
        $l = key( $partialArgs );
30 4
        for ($i = 0; $i <= $l; $i++) {
31 4
            if (!array_key_exists($i, $partialArgs) && count($addedArgs)) {
32 4
                $partialArgs[ $i ] = array_shift( $addedArgs );
33
            }
34
        }
35 4
        if (count($addedArgs)) { // there are $addedArgs left, so there should be no 'holes' in $partialArgs
36
            $partialArgs =array_merge( $partialArgs, $addedArgs );
37
        }
38
        // fill any 'holes' in $partialArgs with entries from $defaultArgs
39 4
        $result =  array_replace( $defaultArgs, $partialArgs );
40 4
        ksort($result);
41
42 4
        return $result;
43 4
    };
44
45 4
    return function() use ($callable, $partialArgs, $defaultArgs, $partialMerge) {
46 4
        if ( $callable instanceof \Closure && isset($this) ) {
0 ignored issues
show
The variable $this seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
47
            $callable = \Closure::bind($callable, $this);
0 ignored issues
show
Consider using a different name than the imported variable $callable, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
48
        }
49 4
        return call_user_func_array( $callable, $partialMerge( $partialArgs, func_get_args(), $defaultArgs ) );
50 4
    };
51
}
52
53
/**
54
 * Class lambda
55
 * Experimental functionality, may be removed later, use at own risk.
56
 * @package arc
57
 */
58
class lambda
59
{
60
    /**
61
     * Returns a function with the given arguments already entered or partially applied.
62
     * @param callable $callable The function to curry
63
     * @param array $partialArgs unlimited Optional arguments to curry the function with
64
     * @param array $defaultArgs optional default values
65
     * @return callable
66
     */
67 4
    public static function partial(callable $callable, $partialArgs, $defaultArgs = [])
68
    {
69 4
        return partial($callable, $partialArgs, $defaultArgs);
70
    }
71
72
73
    /**
74
     * Returns a function with named arguments. The peppered function accepts one argument - a named array of values
75
     * @param callable $callable The function or method to pepper
76
     * @param array $namedArgs Optional. The named arguments to pepper the function with, the order must be the order
77
     *        in which the unpeppered function expects them. If not set, pepper will use Reflection to get them.
78
     *        Format is [ 'argumentName' => 'defaultValue' ]
79
     * @return callable
80
     */
81 2
    public static function pepper(callable $callable, $namedArgs=null)
82
    {
83 2
        if ( !is_array( $namedArgs ) ) {
84
            $ref = !is_array($callable) ? new \ReflectionFunction($callable) : new \ReflectionMethod($callable[0], $callable[1]);
85
            $namedArgs = [];
86
            foreach ($ref->getParameters() as $parameter) {
87
                $namedArgs[ $parameter->getName() ] = $parameter->getDefaultValue();
88
            }
89
        }
90
91 2
        return function ($otherArgs) use ($callable, $namedArgs) {
92 2
            $args = array_values( array_merge( $namedArgs, $otherArgs ) );
93 2
            return call_user_func_array( $callable, $args );
94 2
        };
95
    }
96
97
    /**
98
    * Returns a method that will generate and call the given function only once and return its result for every call.
99
    * The first call generates the result. Each subsequent call simply returns that same result. This allows you
100
    * to create in-context singletons for any kind of object.
101
    * <code>
102
    *   $proto = \arc\lambda::prototype([
103
    *     'getSingleton' => \arc\lambda::singleton( function () {
104
    *       return new ComplexObject();
105
    *     })
106
    *   ]);
107
    * </code>
108
    * @param callable $f The function to generate the singleton.
109
    * @return mixed The singleton.
110
    */
111 2
    public static function singleton($f)
112
    {
113 2
        return singleton($f);
114
    }
115
116
}
117