Issues (25)

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/AppserverIo/Collections/CollectionUtils.php (5 issues)

Labels
Severity

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
/**
4
 * \AppserverIo\Collections\CollectionUtils
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/collections
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Collections;
22
23
use AppserverIo\Lang\Object;
24
25
/**
26
 * This class implements static methods that can be used
27
 * to work with Collections.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2015 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/appserver-io/collections
33
 * @link      http://www.appserver.io
34
 */
35
class CollectionUtils extends Object
36
{
37
38
    /**
39
     * Standardconstructor that marks this class as util class.
40
     */
41
    protected function __construct()
42
    {
43
        /* Marks class as utility */
44
    }
45
46
    /**
47
     * This method iterates over the passed IndexedCollection and invokes the evaluate
48
     * method of the although passed Predicate on every object of the IndexedCollection.
49
     * If the evaluate method returns false, the object is removed from the passed
50
     * IndexedCollection.
51
     *
52
     * @param \AppserverIo\Collections\CollectionInterface $collection Holds the IndexedCollection that should be filtered
53
     * @param \AppserverIo\Collections\PredicateInterface  $predicate  The Predicate that should be used for evaluation purposes
54
     * @param integer                                      $iterations Holds the size of successful iterations, after that the filter should run
55
     *
56
     * @return void
57
     */
58 1
    public static function filter(CollectionInterface $collection, PredicateInterface $predicate, $iterations = 0)
59
    {
60
        // initialize the ArrayList that should be returned
61 1
        $return = array();
62
        // initialize the counter for the iterations
63 1
        $iteration = 0;
64
        // iterate over the ArrayList and invoke the evaluate()
65
        // method of the Predicate on every object of the ArrayList
66 1
        foreach ($collection as $key => $object) {
0 ignored issues
show
The expression $collection of type object<AppserverIo\Colle...ns\CollectionInterface> is not traversable.
Loading history...
67
            // if the Predicate returns true, then adding the object
68
            // to the new ArrayList
69 1
            if ($predicate->evaluate($object)) {
70 1
                $return[$key] = $object;
71
                // rise the iterator
72 1
                $iteration ++;
73
                // if the iterator is equal to the actual iteration number
74
                // then stop
75 1
                if ($iteration == $iterations) {
76
                    break;
77
                }
78 1
            }
79 1
        }
80
        // clear all elements and add the filtered
81 1
        $collection->clear();
82 1
        $collection->addAll($return);
83 1
    }
84
85
    /**
86
     * Finds the first element in the given collection which matches
87
     * the given predicate.
88
     *
89
     * If no element of the collection matches the predicate, null is
90
     * returned.
91
     *
92
     * @param \AppserverIo\Collections\CollectionInterface $collection The collection to search
93
     * @param \AppserverIo\Collections\PredicateInterface  $predicate  The predicate to use
94
     *
95
     * @return object Returns the first element of the collection which matches the predicate or null if none could be found
96
     */
97
    public static function find(CollectionInterface $collection, PredicateInterface $predicate)
98
    {
99
        // iterate over the IndexedCollection and invoke the evaluate()
100
        // method of the Predicate on every object of the IndexedCollection
101
        foreach ($collection as $object) {
0 ignored issues
show
The expression $collection of type object<AppserverIo\Colle...ns\CollectionInterface> is not traversable.
Loading history...
102
            // if the Predicate returns true, if the predicate returns true
103
            if ($predicate->evaluate($object)) {
104
                return $object;
105
            }
106
        }
107
        // return nothing if no object was found
108
        return;
109
    }
110
111
    /**
112
     * This method iterates over the passed Collection and invokes the
113
     * evaluate method of the although passed Predicate on every object
114
     * of the Collection.
115
     * If the evaluate method returns true the method
116
     * returns true also.
117
     *
118
     * @param \AppserverIo\Collections\CollectionInterface $collection Holds the IndexedCollection that should be filtered
119
     * @param \AppserverIo\Collections\PredicateInterface  $predicate  The Predicate that should be used for evaluation purposes
120
     *
121
     * @return boolean TRUE if the evaluate method of the Predicate returns TRUE
122
     */
123
    public static function exists(CollectionInterface $collection, PredicateInterface $predicate)
124
    {
125
        // iterate over the Collection and invoke the evaluate()
126
        // method of the Predicate on every object of the Collection
127
        foreach ($collection as $object) {
0 ignored issues
show
The expression $collection of type object<AppserverIo\Colle...ns\CollectionInterface> is not traversable.
Loading history...
128
            // if the Predicate returns true, if the predicate returns true
129
            if ($predicate->evaluate($object)) {
130
                return true;
131
            }
132
        }
133
        // return false if element is not found
134
        return false;
135
    }
136
137
    /**
138
     * This method iterates over the passed Collection and invokes
139
     * the evaluate method of the although passed Predicate on every object
140
     * of the Collection.
141
     * If the evaluate method returns
142
     * true the method returns the key of the object.
143
     *
144
     * @param \AppserverIo\Collections\CollectionInterface $collection Holds the Collection that should be filtered
145
     * @param \AppserverIo\Collections\PredicateInterface  $predicate  The Predicate that should be used for evaluation purposes
146
     *
147
     * @return mixed Holds the key of the first object with it's evaluate() method returning TRUE
148
     */
149
    public static function findKey(CollectionInterface $collection, PredicateInterface $predicate)
150
    {
151
        // iterate over the IndexedCollection and invoke the evaluate()
152
        // method of the Predicate on every object of the Collection
153
        foreach ($collection as $key => $object) {
0 ignored issues
show
The expression $collection of type object<AppserverIo\Colle...ns\CollectionInterface> is not traversable.
Loading history...
154
            // if the Predicate returns true, if the predicate returns true
155
            if ($predicate->evaluate($object)) {
156
                return $key;
157
            }
158
        }
159
        // return nothing if no object was evaluated by the predicate
160
        return;
161
    }
162
163
    /**
164
     * Returns a new Collection containing a - b.
165
     * The cardinality of each
166
     * element e in the returned Collection will be the cardinality of e
167
     * in a minus the cardinality of e in b, or zero, whichever is greater.
168
     *
169
     * @param \AppserverIo\Collections\CollectionInterface $a The Collection to subtract from, must not be null
170
     * @param \AppserverIo\Collections\CollectionInterface $b The Collection to subtract, must not be null
171
     *
172
     * @return void
173
     */
174
    public static function subtract(CollectionInterface $a, CollectionInterface $b)
175
    {
176
        // initialize the array with the value to return that should be returned
177
        $return = array();
178
        // iterate over the Collection and check if the object exists in
179
        // the second Collection
180
        foreach ($a as $key => $element) {
0 ignored issues
show
The expression $a of type object<AppserverIo\Colle...ns\CollectionInterface> is not traversable.
Loading history...
181
            // if the object does not exist in the second Collection add
182
            // it to the return array
183
            if ($b->exists($key)) {
184
                $return[$key] = $element;
185
            }
186
        }
187
        // clear all elements and add the subtracted
188
        $a->clear();
189
        $a->addAll($return);
190
    }
191
192
    /**
193
     * This method sorts the passed collection depending
194
     * on the comparator.
195
     *
196
     * @param \AppserverIo\Collections\CollectionInterface $collection Holds the Collection that should be sorted
197
     * @param \AppserverIo\Collections\ComparatorInterface $comperator The Comparator that should be used for compare purposes
198
     *
199
     * @return void
200
     */
201 2
    public static function sort(CollectionInterface $collection, ComparatorInterface $comperator)
202
    {
203
        // initialize the ArrayList that should be returned
204
        // sort the ArrayList
205 2
        $return = CollectionUtils::arraySort($collection->toArray(), 0, $collection->size(), $comperator);
206
        // clear all elements and add the sorted
207 2
        $collection->clear();
208 2
        $collection->addAll($return);
209 2
    }
210
211
    /**
212
     * Sorts the passed array.
213
     *
214
     * @param array                                        $src        The Array to be sorted
215
     * @param integer                                      $low        The offset we start sorting
216
     * @param integer                                      $high       The number of elements to be sorted
217
     * @param \AppserverIo\Collections\ComparatorInterface $comperator The comperator used for sorting
218
     *
219
     * @return array The sorted array
220
     */
221 2
    protected static function arraySort($src, $low, $high, ComparatorInterface $comperator)
222
    {
223
        // sort the array
224 2
        for ($i = $low; $i < $high; $i ++) {
225 2
            for ($j = $i; ($j > $low) && ($comperator->compare($src[$j - 1], $src[$j]) > 0); $j --) {
226 2
                $src = CollectionUtils::swap($src, $j, $j - 1);
227 2
            }
228 2
        }
229
        // return the sorted array
230 2
        return $src;
231
    }
232
233
    /**
234
     * Swaps x[a] with x[b].
235
     *
236
     * @param array   $x The array with the values to be swapped
237
     * @param integer $a The position of the element to be swapped
238
     * @param integer $b The position of the element to swap $a with
239
     *
240
     * @return array
241
     */
242 2
    protected static function swap($x, $a, $b)
243
    {
244 2
        $t = $x[$a];
245 2
        $x[$a] = $x[$b];
246 2
        $x[$b] = $t;
247 2
        return $x;
248
    }
249
}
250