1 | <?php |
||||
2 | |||||
3 | |||||
4 | namespace Turahe\Master\Commands; |
||||
5 | |||||
6 | use DateTime; |
||||
7 | use Illuminate\Console\Command; |
||||
8 | use Illuminate\Support\Str; |
||||
9 | |||||
10 | class CurrencyUpdate extends Command |
||||
11 | { |
||||
12 | /** |
||||
13 | * The name and signature of the console command. |
||||
14 | * |
||||
15 | * @var string |
||||
16 | */ |
||||
17 | protected $signature = 'currency:update |
||||
18 | {--e|exchangeratesapi : Get rates from ExchangeRatesApi.io} |
||||
19 | {--o|openexchangerates : Get rates from OpenExchangeRates.org} |
||||
20 | {--g|google : Get rates from Google Finance}'; |
||||
21 | |||||
22 | /** |
||||
23 | * The console command description. |
||||
24 | * |
||||
25 | * @var string |
||||
26 | */ |
||||
27 | protected $description = 'Update exchange rates from an online source'; |
||||
28 | |||||
29 | /** |
||||
30 | * Currency instance |
||||
31 | * |
||||
32 | * @var \Torann\Currency\Currency |
||||
0 ignored issues
–
show
|
|||||
33 | */ |
||||
34 | protected $currency; |
||||
35 | |||||
36 | /** |
||||
37 | * Create a new command instance. |
||||
38 | */ |
||||
39 | public function __construct() |
||||
40 | { |
||||
41 | $this->currency = app('currency'); |
||||
0 ignored issues
–
show
It seems like
app('currency') can also be of type Illuminate\Contracts\Foundation\Application . However, the property $currency is declared as type Torann\Currency\Currency . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
![]() |
|||||
42 | |||||
43 | parent::__construct(); |
||||
44 | } |
||||
45 | |||||
46 | /** |
||||
47 | * Execute the console command for Laravel 5.4 and below |
||||
48 | * |
||||
49 | * @throws \Exception |
||||
50 | * @return void |
||||
51 | */ |
||||
52 | public function fire() |
||||
53 | { |
||||
54 | $this->handle(); |
||||
55 | } |
||||
56 | |||||
57 | /** |
||||
58 | * Execute the console command. |
||||
59 | * |
||||
60 | * @throws \Exception |
||||
61 | * @return void |
||||
62 | */ |
||||
63 | public function handle() |
||||
64 | { |
||||
65 | // Get Settings |
||||
66 | $defaultCurrency = $this->currency->config('default'); |
||||
67 | |||||
68 | if ($this->input->getOption('exchangeratesapi')) { |
||||
69 | // Get rates from exchangeratesapi |
||||
70 | return $this->updateFromExchangeRatesApi($defaultCurrency); |
||||
0 ignored issues
–
show
Are you sure the usage of
$this->updateFromExchang...esApi($defaultCurrency) targeting Turahe\Master\Commands\C...eFromExchangeRatesApi() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
71 | } |
||||
72 | |||||
73 | if ($this->input->getOption('google')) { |
||||
74 | // Get rates from google |
||||
75 | return $this->updateFromGoogle($defaultCurrency); |
||||
0 ignored issues
–
show
Are you sure the usage of
$this->updateFromGoogle($defaultCurrency) targeting Turahe\Master\Commands\C...ate::updateFromGoogle() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
76 | } |
||||
77 | |||||
78 | if ($this->input->getOption('openexchangerates')) { |
||||
79 | if (! $api = $this->currency->config('api_key')) { |
||||
80 | $this->error('An API key is needed from OpenExchangeRates.org to continue.'); |
||||
81 | |||||
82 | return; |
||||
83 | } |
||||
84 | |||||
85 | // Get rates from OpenExchangeRates |
||||
86 | return $this->updateFromOpenExchangeRates($defaultCurrency, $api); |
||||
0 ignored issues
–
show
Are you sure the usage of
$this->updateFromOpenExc...$defaultCurrency, $api) targeting Turahe\Master\Commands\C...FromOpenExchangeRates() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
87 | } |
||||
88 | } |
||||
89 | |||||
90 | /** |
||||
91 | * Fetch rates from the API |
||||
92 | * |
||||
93 | * @param $defaultCurrency |
||||
94 | */ |
||||
95 | private function updateFromExchangeRatesApi($defaultCurrency) |
||||
96 | { |
||||
97 | $this->info('Updating currency exchange rates from ExchangeRatesApi.io...'); |
||||
98 | |||||
99 | // Make request |
||||
100 | $content = json_decode($this->request("https://api.exchangeratesapi.io/latest?base={$defaultCurrency}")); |
||||
101 | |||||
102 | // Error getting content? |
||||
103 | if (isset($content->error)) { |
||||
104 | $this->error($content->description); |
||||
105 | |||||
106 | return; |
||||
107 | } |
||||
108 | |||||
109 | // Update each rate |
||||
110 | foreach ($content->rates as $code => $value) { |
||||
111 | $this->currency->getDriver()->update($code, [ |
||||
112 | 'exchange_rate' => $value, |
||||
113 | ]); |
||||
114 | } |
||||
115 | |||||
116 | $this->currency->clearCache(); |
||||
117 | |||||
118 | $this->info('Update!'); |
||||
119 | } |
||||
120 | |||||
121 | /** |
||||
122 | * Fetch rates from the API |
||||
123 | * |
||||
124 | * @param $defaultCurrency |
||||
125 | * @param $api |
||||
126 | * |
||||
127 | * @throws \Exception |
||||
128 | */ |
||||
129 | private function updateFromOpenExchangeRates($defaultCurrency, $api) |
||||
130 | { |
||||
131 | $this->info('Updating currency exchange rates from OpenExchangeRates.org...'); |
||||
132 | |||||
133 | // Make request |
||||
134 | $content = json_decode( |
||||
135 | $this->request("http://openexchangerates.org/api/latest.json?base={$defaultCurrency}&app_id={$api}&show_alternative=1") |
||||
136 | ); |
||||
137 | |||||
138 | // Error getting content? |
||||
139 | if (isset($content->error)) { |
||||
140 | $this->error($content->description); |
||||
141 | |||||
142 | return; |
||||
143 | } |
||||
144 | |||||
145 | // Parse timestamp for DB |
||||
146 | $timestamp = (new DateTime())->setTimestamp($content->timestamp); |
||||
147 | |||||
148 | // Update each rate |
||||
149 | foreach ($content->rates as $code => $value) { |
||||
150 | $this->currency->getDriver()->update($code, [ |
||||
151 | 'exchange_rate' => $value, |
||||
152 | 'updated_at' => $timestamp, |
||||
153 | ]); |
||||
154 | } |
||||
155 | |||||
156 | $this->currency->clearCache(); |
||||
157 | |||||
158 | $this->info('Update!'); |
||||
159 | } |
||||
160 | |||||
161 | /** |
||||
162 | * Fetch rates from Google Finance |
||||
163 | * |
||||
164 | * @param $defaultCurrency |
||||
165 | */ |
||||
166 | private function updateFromGoogle($defaultCurrency) |
||||
167 | { |
||||
168 | $this->info('Updating currency exchange rates from finance.google.com...'); |
||||
169 | |||||
170 | foreach ($this->currency->getDriver()->all() as $code => $value) { |
||||
171 | // Don't update the default currency, the value is always 1 |
||||
172 | if ($code === $defaultCurrency) { |
||||
173 | continue; |
||||
174 | } |
||||
175 | |||||
176 | $response = $this->request('http://finance.google.com/finance/converter?a=1&from=' . $defaultCurrency . '&to=' . $code); |
||||
177 | |||||
178 | if (Str::contains($response, 'bld>')) { |
||||
179 | $data = explode('bld>', $response); |
||||
180 | $rate = explode($code, $data[1])[0]; |
||||
181 | |||||
182 | $this->currency->getDriver()->update($code, [ |
||||
183 | 'exchange_rate' => $rate, |
||||
184 | ]); |
||||
185 | } else { |
||||
186 | $this->warn('Can\'t update rate for ' . $code); |
||||
187 | continue; |
||||
188 | } |
||||
189 | } |
||||
190 | } |
||||
191 | |||||
192 | /** |
||||
193 | * Make the request to the sever. |
||||
194 | * |
||||
195 | * @param $url |
||||
196 | * |
||||
197 | * @return string |
||||
198 | */ |
||||
199 | private function request($url) |
||||
200 | { |
||||
201 | $ch = curl_init($url); |
||||
202 | |||||
203 | curl_setopt($ch, CURLOPT_HEADER, false); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_setopt() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
204 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); |
||||
205 | curl_setopt($ch, CURLOPT_TIMEOUT, 20); |
||||
206 | curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); |
||||
207 | curl_setopt($ch, CURLOPT_HTTPGET, true); |
||||
208 | curl_setopt($ch, CURLOPT_MAXREDIRS, 2); |
||||
209 | curl_setopt($ch, CURLOPT_MAXCONNECTS, 2); |
||||
210 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||||
211 | |||||
212 | $response = curl_exec($ch); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_exec() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
213 | curl_close($ch); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_close() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
214 | |||||
215 | return $response; |
||||
216 | } |
||||
217 | } |
||||
218 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths