Issues (9)

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/Service/Factory.php (2 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
declare(strict_types=1);
4
5
/*
6
 * This file is part of Swap.
7
 *
8
 * (c) Florian Voutzinos <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Swap\Service;
15
16
use Exchanger\Service\HttpService;
17
use Http\Client\HttpClient;
18
use Http\Discovery\HttpClientDiscovery;
19
use Http\Discovery\Psr17FactoryDiscovery;
20
use Psr\Http\Client\ClientInterface;
21
use Http\Message\RequestFactory;
22
use Psr\Http\Message\RequestFactoryInterface;
23
24
/**
25
 * Helps building services.
26
 *
27
 * @author Florian Voutzinos <[email protected]>
28
 */
29
final class Factory
30
{
31
    /**
32
     * The client.
33
     *
34
     * @var HttpClient
35
     */
36
    private $httpClient;
37
38
    /**
39
     * The request factory.
40
     *
41
     * @var RequestFactory
42
     */
43
    private $requestFactory;
44
45
    /**
46
     * The service registry.
47
     *
48
     * @var Registry
49
     */
50
    private $registry;
51
52
    /**
53
     * @param HttpClient|ClientInterface|null $httpClient
54
     * @param RequestFactoryInterface|null    $requestFactory
55
     */
56
    public function __construct($httpClient = null, RequestFactoryInterface $requestFactory = null)
57
    {
58
        if (null === $httpClient) {
59
            $httpClient = HttpClientDiscovery::find();
60
        } else {
61
            if (!$httpClient instanceof ClientInterface && !$httpClient instanceof HttpClient) {
62
                throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
63
            }
64
        }
65
66
        $this->httpClient = $httpClient;
0 ignored issues
show
Documentation Bug introduced by florianv
It seems like $httpClient can also be of type object<Psr\Http\Client\ClientInterface>. However, the property $httpClient is declared as type object<Http\Client\HttpClient>. 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 $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

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;
}
Loading history...
67
        $this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
68
        $this->registry = new Registry();
69
    }
70
71
    /**
72
     * Sets the http client.
73
     *
74
     * @param HttpClient|ClientInterface $httpClient
75
     */
76 View Code Duplication
    public function setHttpClient($httpClient): void
77
    {
78
        if (!$httpClient instanceof ClientInterface && !$httpClient instanceof HttpClient) {
79
            throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
80
        }
81
82
        $this->httpClient = $httpClient;
0 ignored issues
show
Documentation Bug introduced by florianv
$httpClient is of type object<Psr\Http\Client\ClientInterface>, but the property $httpClient was declared to be of type object<Http\Client\HttpClient>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
83
    }
84
85
    /**
86
     * Sets the request factory.
87
     *
88
     * @param RequestFactoryInterface $requestFactory
89
     */
90
    public function setRequestFactory(RequestFactoryInterface $requestFactory): void
91
    {
92
        $this->requestFactory = $requestFactory;
93
    }
94
95
    /**
96
     * Creates a new service.
97
     *
98
     * @param string $serviceName
99
     * @param array  $args
100
     *
101
     * @return \Exchanger\Contract\ExchangeRateService
102
     */
103
    public function create(string $serviceName, array $args = [])
104
    {
105
        if (!$this->registry->has($serviceName)) {
106
            throw new \LogicException(sprintf('The service "%s" is not registered.', $serviceName));
107
        }
108
109
        $classOrCallable = $this->registry->get($serviceName);
110
111
        if (is_callable($classOrCallable)) {
112
            return call_user_func($classOrCallable);
113
        }
114
115
        if (is_subclass_of($classOrCallable, HttpService::class)) {
116
            return new $classOrCallable($this->httpClient, $this->requestFactory, $args);
117
        }
118
119
        $r = new \ReflectionClass($classOrCallable);
120
121
        return $r->newInstanceArgs($args);
122
    }
123
}
124