MongoId::toHex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
namespace PHPDaemon\Clients\Mongo;
3
4
/**
5
 * @package    Applications
6
 * @subpackage MongoClientAsync
7
 * @author     Vasily Zorin <[email protected]>
8
 */
9
class MongoId extends \MongoId
10
{
11
12
    /**
13
     * @param string $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
14
     */
15
    public function __construct($id = null)
16
    {
17
        if ($id !== null && mb_orig_strlen($id) < 20 && ctype_alnum($id)) {
18
            $id = gmp_strval(gmp_init(strrev($id), 62), 16);
19
            if (mb_orig_strlen($id) > 24) {
20
                $id = 'FFFFFFFFFFFFFFFFFFFFFFFF';
21
            } elseif (mb_orig_strlen($id) < 24) {
22
                $id = str_pad($id, 24, '0', STR_PAD_LEFT);
23
            }
24
        }
25
        @parent::__construct($id);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
26
    }
27
28
    /**
29
     * Import
30
     * @param  mixed $id ID
31
     * @return mixed
32
     */
33
    public static function import($id)
34
    {
35
        if ($id instanceof static) {
36
            return $id;
37
        } elseif ($id instanceof \MongoId) {
38
            $id = (string)$id;
39
        } elseif (!is_string($id)) {
40
            if (is_array($id) && isset($id['$id'])) {
41
                return static::import($id['$id']);
42
            }
43
            return false;
44
        } elseif (mb_orig_strlen($id) === 24) {
45
            if (!ctype_xdigit($id)) {
46
                return false;
47
            }
48
        } elseif (ctype_alnum($id)) {
49
            $id = gmp_strval(gmp_init(strrev($id), 62), 16);
50
            if (mb_orig_strlen($id) > 24) {
51
                return false;
52
            }
53
            if (mb_orig_strlen($id) < 24) {
54
                $id = str_pad($id, 24, '0', STR_PAD_LEFT);
55
            }
56
        } else {
57
            return false;
58
        }
59
        return new static($id);
60
    }
61
62
    /**
63
     * __toString
64
     * @return string
65
     */
66
    public function __toString()
67
    {
68
        return strrev(gmp_strval(gmp_init(parent::__toString(), 16), 62));
69
    }
70
71
    /**
72
     * toHex
73
     * @return string
74
     */
75
    public function toHex()
76
    {
77
        return parent::__toString();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__toString() instead of toHex()). Are you sure this is correct? If so, you might want to change this to $this->__toString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
78
    }
79
80
    /**
81
     * getPlainObject
82
     * @return \MongoId
83
     */
84
    public function getPlainObject()
85
    {
86
        return new \MongoId(parent::__toString());
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__toString() instead of getPlainObject()). Are you sure this is correct? If so, you might want to change this to $this->__toString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
87
    }
88
}
89