DatabaseConnect::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 3 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
class DatabaseConnect extends mysqli
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
 // The database connection
6
	public function __construct()
7
	{
8
		$config = parse_ini_file('config/config.ini.php'); 
9
		
10
		parent::__construct($config['host'], $config['username'], $config['password'], $config['dbname']);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class mysqli as the method __construct() does only exist in the following sub-classes of mysqli: DatabaseConnect. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
11
	}
12
13
}
14
$db = new DatabaseConnect();
15
16
if($db->connect_errno == 1203)  // 1203 == ER_TOO_MANY_USER_CONNECTIONS (mysqld_error.h)
17
{
18
	error_log('ER_TOO_MANY_USER_CONNECTIONS');
19
	sleep(1);
20
	header('location: '.$_SERVER['PHP_SELF']);
21
}
22
23
if ($db->connect_errno)
24
{
25
	error_log('Sorry, die Verbindung zu unserem 
26
        Server ist hops gegangen. Wegen '. $db -> connect_error);
27
	echo 'Sorry, die Verbindung zu unserem 
28
        Server ist hops gegangen. Wegen '. $db -> connect_error;
29
}
30
31
32
/* Uncomment if you start first time
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
$query = "CREATE TABLE IF NOT EXISTS `accounts` (
34
			 `id` int(11) unsigned NOT NULL auto_increment,
35
			 `access_token` varchar(55) NOT NULL default '',
36
			 `refresh_token` varchar(55) NOT NULL default '',
37
			 `token_type` varchar(55) NOT NULL default 'bearer',
38
			 `expires_in` INT NOT NULL,
39
			 `expiration_date` INT NOT NULL,
40
			 `device_uid` varchar(255) NOT NULL default '',
41
			 `client_id` varchar(255) NOT NULL default '81e8a76e-1e02-4d17-9ba0-8a7020261b26',
42
			 `distinct_id` varchar(255) NOT NULL default '',
43
			 `city` varchar(100) default '',
44
			 `country` varchar(10) default 'DE',
45
			 `loc_accuracy` varchar(50) default '0.0',
46
			 `lat` varchar(255) default '',
47
			 `lng` varchar(255) default '',
48
			 `name` varchar(100) default '',
49
			 `X-Client-Type` varchar(50) NOT NULL default 'android_4.24.2',
50
			 `User-Agent` varchar(150) NOT NULL default 'Jodel/4.4.9 Dalvik/2.1.0 (Linux; U; Android 5.1.1; )',
51
			 `X-Api-Version` varchar(10) NOT NULL default '0.2',
52
			 PRIMARY KEY  (`id`)
53
			 ) DEFAULT CHARSET=utf8";
54
$query2 = "CREATE TABLE IF NOT EXISTS `votes` (
55
			 `id` int(11) unsigned NOT NULL auto_increment,
56
			 `device_uid` varchar(255) NOT NULL,
57
			 `postId` varchar(255) NOT NULL,
58
			 `type` varchar(255) NOT NULL,
59
			 PRIMARY KEY  (`id`)
60
			 ) DEFAULT CHARSET=utf8";
61
62
$query3	 = "CREATE TABLE IF NOT EXISTS `users` (
63
			 `id` int(11) unsigned NOT NULL auto_increment,
64
			 `device_uid` varchar(255) NOT NULL,
65
			 `rights` varchar(255) NOT NULL,
66
			 `user_token` varchar(255) NOT NULL,
67
			 `remaining_votes` INT NOT NULL,
68
			 PRIMARY KEY  (`id`)
69
			 ) DEFAULT CHARSET=utf8";
70
71
$queryCitys	 = "CREATE TABLE IF NOT EXISTS `citys` (
72
			 `id` int(11) unsigned NOT NULL auto_increment,
73
			 `name` varchar(255) NOT NULL,
74
			 `lat` varchar(255) NOT NULL,
75
			 `lng` varchar(255) NOT NULL,
76
			 `country` INT NOT NULL,
77
			 PRIMARY KEY  (`id`)
78
			 ) DEFAULT CHARSET=utf8";
79
		 
80
  if(!$db->query($query) || !$db->query($query2) || !$db->query($query3) || !$db->query($queryCitys))
81
  {
82
    throw new Exception($db->error($mysqli));
83
  }
84
*/