Profiler::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 9
dl 0
loc 6
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Sitetheory\Bundle\ProfilerStorageBundle\Profiler;
4
5
use Psr\Log\LoggerInterface;
6
use Symfony\Component\HttpKernel\Profiler\Profiler as ProfilerBase;
7
use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Sitetheory\Bundle\Profil...rofilerStorageInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
/**
10
 * Class Profiler.
11
 */
12
class Profiler extends ProfilerBase
13
{
14
    /**
15
     * Profiler constructor.
16
     *
17
     * @param ProfilerStorageInterface $storage
18
     * @param LoggerInterface          $logger
19
     * @param bool                     $enable
20
     * @param bool                     $defaultStorage
21
     * @param null                     $class
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $class is correct as it would always require null to be passed?
Loading history...
22
     * @param null                     $dsn
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $dsn is correct as it would always require null to be passed?
Loading history...
23
     * @param null                     $username
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $username is correct as it would always require null to be passed?
Loading history...
24
     * @param null                     $password
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $password is correct as it would always require null to be passed?
Loading history...
25
     * @param int                      $ttl
26
     */
27
    public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger, $enable = true, $defaultStorage = true, $class = null, $dsn = null, $username = null, $password = null, $ttl = 3600)
28
    {
29
        if (true !== $defaultStorage) {
30
            $storage = new $class($dsn, $username, $password, $ttl);
31
        }
32
        parent::__construct($storage, $logger, $enable);
33
    }
34
}
35