JsonTable::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Kir\MySQL\Tools;
4
5
use Kir\MySQL\Common\SpecialTable;
6
use Kir\MySQL\Database;
7
8
/**
9
 * @phpstan-type TColumn string|array{name: string, type: string, jsonPath: string}
10
 *
11
 * @phpstan-type TColumns list<string|TColumn>
12
 */
13
class JsonTable implements SpecialTable {
14
	/**
15
	 * @param string $dataExpression The field to get the source json data from
16
	 * @param string $jsonPath The json path to the array to be turned into a table
17
	 * @param TColumns $columns The projected columns
0 ignored issues
show
Bug introduced by
The type Kir\MySQL\Tools\TColumns was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
	 */
19
	public function __construct(
20
		private string $dataExpression,
21
		private string $jsonPath,
22
		private $columns,
23
	) {}
24
25
	public function asString(Database $db): string {
26
		return sprintf(
27
			'JSON_TABLE(%s, %s, %s)',
28
			$this->dataExpression,
29
			$this->jsonPath,
30
			$this->translateColumns($db, $this->columns)
31
		);
32
	}
33
34
	/**
35
	 * @param Database $db
36
	 * @param TColumns $columns
37
	 * @return string
38
	 */
39
	private function translateColumns(Database $db, $columns) {
0 ignored issues
show
Unused Code introduced by
The parameter $db is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

39
	private function translateColumns(/** @scrutinizer ignore-unused */ Database $db, $columns) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
		$result = [];
41
		foreach($columns as $column) {
42
			if(!is_string($column)) {
43
				$column = sprintf('%s %s PATH \'%s\'', $column['name'], $column['type'], $column['jsonPath']);
44
			}
45
			$result[] = $column;
46
		}
47
48
		return sprintf('COLUMNS(%s)', implode(', ', $result));
49
	}
50
}
51