Completed
Push — PSR-11-2 ( a5ad88...7f5041 )
by Nikolaos
03:59
created

SerializerFactory::newInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 4
cts 6
cp 0.6667
rs 10
cc 1
nc 1
nop 1
crap 1.037
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Storage;
15
16
use Phalcon\Factory\AbstractFactory;
17
use Phalcon\Factory\Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Phalcon\Storage\Exception.

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...
18
use Phalcon\Storage\Serializer\SerializerInterface;
19
20
/**
21
 * Class SerializerFactory
22
 *
23
 * @package Phalcon\Storage
24
 */
25
class SerializerFactory extends AbstractFactory
26
{
27
    /**
28
     * SerializerFactory constructor.
29
     *
30
     * @param array $services
31
     */
32 190
    public function __construct(array $services = [])
33
    {
34 190
        $this->init($services);
35 190
    }
36
37
    /**
38
     * @param string $name
39
     *
40
     * @return SerializerInterface
41
     * @throws Exception
42
     */
43 120
    public function newInstance(string $name): SerializerInterface
44
    {
45 120
        $this->checkService($name);
46
47 119
        $definition = $this->mapper[$name];
48
49 119
        return new $definition();
50
    }
51
52
    /**
53
     * @return array
54
     */
55 190
    protected function getAdapters(): array
56
    {
57
        return [
58 190
            "base64"   => 'Phalcon\Storage\Serializer\Base64',
59
            "igbinary" => 'Phalcon\Storage\Serializer\Igbinary',
60
            "json"     => 'Phalcon\Storage\Serializer\Json',
61
            "msgpack"  => 'Phalcon\Storage\Serializer\Msgpack',
62
            "none"     => 'Phalcon\Storage\Serializer\None',
63
            "php"      => 'Phalcon\Storage\Serializer\Php',
64
        ];
65
    }
66
}
67