Issues (5)

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.

tests/QuandlTest.php (5 issues)

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
namespace Royopa\Quandl\Tests;
4
5
use Royopa\Quandl\Quandl;
6
7
class QuandlTest extends \PHPUnit_Framework_TestCase
8
{
9
    private $api_key  = "DEBUG_KEY";
10
11
    private $symbol     = 'WIKI/AAPL';
12
13
    private $symbols    = [
14
        'WIKI/CSCO',
15
        'WIKI/AAPL'
16
    ];
17
18
    private $dates      = [
19
        'trim_start' => '2014-01-01',
20
        'trim_end'   => '2014-02-02'
21
    ];
22
23
    protected function setUp()
24
    {
25
        if (! file_exists(__DIR__.'/../.env')) {
26
            return;
27
        }
28
29
        $dotenv = new \Dotenv\Dotenv(__DIR__.'/../');
30
        $dotenv->load();
31
        $this->api_key = getenv('QUANDL_API_KEY');
32
    }
33
34
    private $cache_file = false;
35
36
    public function tearDown()
37
    {
38
        $this->cache_file and unlink($this->cache_file);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
39
    }
40
41
    public function testCsv()
42
    {
43
        $this->_testGetSymbol("csv", 2800);
44
        $this->_testGetSymbol("csv", 2800, true);
45
    }
46
47
    public function testXml()
48
    {
49
        $this->_testGetSymbol("xml", 14000);
50
        $this->_testGetSymbol("xml", 14000, true);
51
    }
52
53
    public function testJson()
54
    {
55
        $this->_testGetSymbol("json", 4200);
56
        $this->_testGetSymbol("json", 4200, true);
57
    }
58
59
    public function testObject()
60
    {
61
        $this->_testGetSymbol("object", 7400);
62
        $this->_testGetSymbol("object", 7400, true);
63
    }
64
65
    public function testInvalidUrl()
66
    {
67
        $this->_testInvalidUrl();
68
        $this->_testInvalidUrl(true);
69
    }
70
71
    public function testCache()
72
    {
73
        $this->_testCache();
74
        $this->cache_file and unlink($this->cache_file);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
75
        $this->_testCache(true);
76
    }
77
78
    public function cacheHandler($action, $url, $data=null)
79
    {
80
        $cache_key  = md5("quandl:$url");
81
        $cache_file = __DIR__ . "/$cache_key";
82
83
        if($action == "get" and file_exists($cache_file)) 
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
84
            return file_get_contents($cache_file);
85
        else if($action == "set") 
86
            file_put_contents($cache_file, $data);
87
88
        $this->cache_file = $cache_file;
0 ignored issues
show
Documentation Bug introduced by
The property $cache_file was declared of type boolean, but $cache_file is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
89
        
90
        return false;
91
    }
92
93
    private function _testInvalidUrl($force_curl = false)
94
    {
95
        $quandl = new Quandl($this->api_key, "json");
96
        $quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
97
        $result = $quandl->getSymbol("INVALID/SYMBOL", $this->dates);
0 ignored issues
show
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
        
99
        $this->assertEquals(
100
            $quandl->error,
101
            "URL not found or invalid URL", 
102
            "TEST invalidUrl response"
103
        );
104
    }
105
106
    private function _testGetList($force_curl = false)
107
    {
108
        $quandl = new Quandl($this->api_key);
109
        $quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
110
        $result = $quandl->getList("WIKI", 1, 223);
111
        
112
        $this->assertEquals(
113
            223,
114
            count($result->docs),
115
            "TEST getList count"
116
        );
117
    }
118
119
    private function _testGetSearch($force_curl = false)
120
    {
121
        $quandl = new Quandl($this->api_key);
122
        $quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
123
        $result = $quandl->getSearch("crud oil", 1, 220);
124
        
125
        $this->assertEquals(220, count($result->docs), "TEST getSearch count");
126
    }
127
128
    private function _testCache($force_curl = false)
129
    {
130
        $quandl = new Quandl($this->api_key);
131
        $quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
132
        $quandl->cache_handler = array($this, "cacheHandler");
133
        $result = $quandl->getSymbol($this->symbol, $this->dates);
134
        $count = count($result->data);
135
        
136
        $this->assertFalse($quandl->was_cached, "TEST was_cache should be false");
137
138
        $result = $quandl->getSymbol($this->symbol, $this->dates);
139
        
140
        $this->assertEquals(
141
        	$count,
142
        	count($result->data), 
143
            "TEST count before and after cache should match"
144
		);
145
146
        $this->assertTrue($quandl->was_cached, "TEST was_cache should be true");
147
    }
148
149
    /**
150
     * @param string $format
151
     * @param integer $length
152
     */
153
    private function _testGetSymbol($format, $length, $force_curl = false)
154
    {
155
        $quandl = new Quandl($this->api_key, $format);
156
        $quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
157
        $result = $quandl->getSymbol($this->symbol, $this->dates);
158
        $quandl_format = $format;
159
        
160
        if(is_object($result)) {
161
            $result = serialize($result);
162
            $quandl_format = "json";
163
        }
164
165
        $this->assertGreaterThan(
166
            $length,
167
            strlen($result), 
168
            "TEST $format length");
169
170
        $url_expected = "https://www.quandl.com/api/v1/datasets/{$this->symbol}.{$quandl_format}?trim_start={$this->dates['trim_start']}&trim_end={$this->dates['trim_end']}&auth_token={$this->api_key}";
171
        
172
        $this->assertEquals(
173
            $url_expected,
174
            $quandl->last_url,
175
            "TEST $format url");
176
    }
177
}
178