Completed
Push — master ( d1730c...d58317 )
by BENOIT
26:43
created

InsertQueryStringifier::buildValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BenTools\Where\InsertQuery;
5
6
class InsertQueryStringifier
7
{
8
9
    /**
10
     * @param InsertQueryBuilder $query
11
     * @return array
12
     */
13
    private static function initBuild(InsertQueryBuilder $query): array
14
    {
15
        $parts = array_merge([$query->mainKeyword], $query->flags);
0 ignored issues
show
Documentation introduced by
The property $mainKeyword is declared private in BenTools\Where\InsertQuery\InsertQueryBuilder. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property $flags is declared private in BenTools\Where\InsertQuery\InsertQueryBuilder. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
16
        return $parts;
17
    }
18
19
    /**
20
     * @param InsertQueryBuilder $insertQuery
21
     * @param array              $parts
22
     */
23
    private static function buildTable(InsertQueryBuilder $insertQuery, array &$parts)
24
    {
25
        if (null !== $insertQuery->table) {
0 ignored issues
show
Documentation introduced by
The property $table is declared private in BenTools\Where\InsertQuery\InsertQueryBuilder. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
26
            $parts[] = sprintf('INTO %s', $insertQuery->escape($insertQuery->table));
0 ignored issues
show
Documentation introduced by
The property $table is declared private in BenTools\Where\InsertQuery\InsertQueryBuilder. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
        }
28
    }
29
30
    /**
31
     * @param InsertQueryBuilder $insertQuery
32
     * @param array              $parts
33
     */
34
    private static function buildColumns(InsertQueryBuilder $insertQuery, array &$parts)
35
    {
36
        $columns = $insertQuery->getColumns();
37
        if (null !== $columns) {
38
            $parts[] = sprintf('(%s)', implode(', ', array_map([$insertQuery, 'escape'], $columns)));
39
        }
40
    }
41
42
    /**
43
     * @param InsertQueryBuilder $insertQuery
44
     * @param array              $parts
45
     */
46
    private static function buildValues(InsertQueryBuilder $insertQuery, array &$parts)
47
    {
48
        $columns = $insertQuery->getColumns();
49
50
        $parts[] = 'VALUES';
51
52
        $nbColumns = count($columns);
53
        $nbValues = count($insertQuery->values);
0 ignored issues
show
Documentation introduced by
The property $values is declared private in BenTools\Where\InsertQuery\InsertQueryBuilder. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
        $pattern = sprintf('(%s)', implode(', ', array_fill(0, $nbColumns, '?')));
55
        $valueParts = array_fill(0, $nbValues, $pattern);
56
57
        $parts[] = implode(', ', $valueParts);
58
    }
59
60
    /**
61
     * @param InsertQueryBuilder $insertQuery
62
     * @param array              $parts
63
     */
64
    private static function buildDuplicateConditions(InsertQueryBuilder $insertQuery, array &$parts)
65
    {
66
        $duplicateConditions = $insertQuery->onDuplicate;
0 ignored issues
show
Documentation introduced by
The property $onDuplicate is declared private in BenTools\Where\InsertQuery\InsertQueryBuilder. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
        if ([] !== $duplicateConditions && null !== $duplicateConditions) {
68
            $parts[] = 'ON DUPLICATE KEY UPDATE';
69
            $updateParts = [];
70
            array_walk($duplicateConditions, function ($value, $key) use ($insertQuery, &$updateParts) {
71
                $updateParts[] = sprintf('%s = %s', $insertQuery->escape($key), $value);
72
            });
73
74
            $parts[] = implode(', ', $updateParts);
75
        }
76
    }
77
78
    /**
79
     * @param InsertQueryBuilder $insertQuery
80
     * @return string
81
     */
82
    public static function stringify(InsertQueryBuilder $insertQuery): string
83
    {
84
        $parts = self::initBuild($insertQuery);
85
        self::buildTable($insertQuery, $parts);
86
        self::buildColumns($insertQuery, $parts);
87
        self::buildValues($insertQuery, $parts);
88
        self::buildDuplicateConditions($insertQuery, $parts);
89
        return implode(' ', $parts) . $insertQuery->end;
0 ignored issues
show
Documentation introduced by
The property $end is declared private in BenTools\Where\InsertQuery\InsertQueryBuilder. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
    }
91
}
92