Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ on:

jobs:
tests:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
strategy:
matrix:
php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
name: PHP ${{ matrix.php }}
steps:
- uses: actions/checkout@v3
# required for "git tag" presence for MonorepoBuilder split and ChangelogLinker git tags resolver; default is 1
- run: git fetch --depth=100000 origin
- uses: actions/checkout@v4
with:
fetch-depth: 5
# see https://github.com/shivammathur/setup-php
- uses: shivammathur/setup-php@v2
with:
Expand All @@ -25,10 +25,11 @@ jobs:
- run: composer phpunit

tests_lowest_dependencies:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- run: git fetch --depth=100000 origin
- uses: actions/checkout@v4
with:
fetch-depth: 5
# see https://github.com/shivammathur/setup-php
- uses: shivammathur/setup-php@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan": "^1.4",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0"
Expand Down
34 changes: 14 additions & 20 deletions lib/MC/Google/Visualization.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
use MC\Parser\Token;
use PDO;
use PDOException;
use Tests\VisualizationTest;

/**
* Provide a working implementation of the Google Visualization Query data source that works with a database
* (or any other custom backend). The documentation for the query language itself and how to use it with Google
* Visualizations can be found here: http://code.google.com/apis/visualization/documentation/querylanguage.html.
*
* @see \Tests\VisualizationTest
* @see VisualizationTest
*
* @phpstan-type FieldSpec array{
* type: string,
Expand Down Expand Up @@ -106,10 +107,8 @@ class Visualization
* Create a new instance. This must be done before the library can be used. Pass in a PDO connection and
* dialect if MC_Google_Visualization will handle the entire request cycle.
*
* @param null|PDO $db the database connection to use
* @param string $dialect the SQL dialect to use - one of "mysql", "postgres", or "sqlite"
*
* @phpstan-param 'mysql'|'postgres'|'sqlite' $dialect
* @param null|PDO $db the database connection to use
* @param 'mysql'|'postgres'|'sqlite' $dialect the SQL dialect to use
*
* @throws Visualization_Error
*/
Expand Down Expand Up @@ -140,9 +139,7 @@ public function setDB(?PDO $db = null): void
/**
* Set the dialect to use when generating SQL statements.
*
* @param string $dialect one of "mysql", "postgres", or "sqlite"
*
* @phpstan-param 'mysql'|'postgres'|'sqlite' $dialect
* @param 'mysql'|'postgres'|'sqlite' $dialect the SQL dialect to use
*
* @throws Visualization_Error
*/
Expand All @@ -158,11 +155,8 @@ public function setSqlDialect(string $dialect): void
/**
* Change the default format string to use for a particular data type.
*
* @param string $type the data type to change - one of "date", "datetime", "time", "boolean", or "number"
*
* @phpstan-param 'date'|'datetime'|'time'|'boolean'|'number' $type
*
* @param string $format the format string to use for the data type
* @param string $format the format string to use for the data type
* @param 'boolean'|'date'|'datetime'|'number'|'time' $type the data type to change
*
* @throws Visualization_Error
*/
Expand Down Expand Up @@ -582,7 +576,7 @@ public function getRowValues(array $row, array $meta): string
$time = strtotime($year.'0104 +'.$week.' weeks');
assert(false !== $time);
$monday = strtotime('-'.((int) date('w', $time) - 1).' days', $time);
assert(false !== $monday); // @phpstan-ignore-line ; PHP < 8.0
assert(false !== $monday);
[$year, $month, $day] = explode('-', date('Y-m-d', $monday));
$formatted = date($format, $monday);
} else {
Expand Down Expand Up @@ -737,7 +731,7 @@ public function getGrammar(): Def
*
* @throws ParseError
* @throws Visualization_QueryError
* @throws Parser\DefError
* @throws DefError
*/
public function parseQuery(string $str): array
{
Expand Down Expand Up @@ -887,7 +881,7 @@ public function parseQuery(string $str): array
*
* @return string the string to output that will cause the visualization client to detect an error
*/
protected function handleError(int $reqid, string $detailMsg, string $handler = 'google.visualization.Query.setResponse', string $code = 'error', string $summaryMsg = null): string
protected function handleError(int $reqid, string $detailMsg, string $handler = 'google.visualization.Query.setResponse', string $code = 'error', ?string $summaryMsg = null): string
{
if (null === $summaryMsg) {
$summaryMsg = $detailMsg;
Expand Down Expand Up @@ -1013,7 +1007,7 @@ protected function generateSQL(array &$meta): string
$stmt = $this->db->query($pivotSql);
assert(false !== $stmt);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
assert(false !== $rows); // @phpstan-ignore-line ; PHP < 8.0
assert(false !== $rows);
foreach ($rows as $row) {
// Create a version of all function-ed fields for each unique combination of pivot values
foreach ($funcFields as $field) {
Expand Down Expand Up @@ -1292,7 +1286,7 @@ protected function getFieldQuote(): string
*
* @return string the SQL string for this field, with an op
*/
protected function getFieldSQL(string $name, array $spec, bool $alias = false, string $func = null, array $pivot = null, array $pivotFields = null): string
protected function getFieldSQL(string $name, array $spec, bool $alias = false, ?string $func = null, ?array $pivot = null, ?array $pivotFields = null): string
{
$sql = $spec['field'];
$q = $this->getFieldQuote();
Expand Down Expand Up @@ -1360,7 +1354,7 @@ protected function addDependantCallbackFields(array $fieldSpec, array $entity, a
* @param Token $token the token or token group to recursively parse
* @param null|array $fields the collector array reference to receive the flattened select field values
*/
protected function parseFieldTokens(Token $token, array &$fields = null): void
protected function parseFieldTokens(Token $token, ?array &$fields = null): void
{
if ('*' === $token->value) {
return;
Expand Down Expand Up @@ -1392,7 +1386,7 @@ protected function parseFieldTokens(Token $token, array &$fields = null): void
* @param Token $token the token or token group to parse
* @param null|array<array{type: string, value: string}> $where the collector array of tokens that make up the where clause
*/
protected function parseWhereTokens(Token $token, array &$where = null): void
protected function parseWhereTokens(Token $token, ?array &$where = null): void
{
if (!is_array($where)) {
$where = [];
Expand Down
5 changes: 3 additions & 2 deletions lib/MC/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
use MC\Parser\Def\Set;
use MC\Parser\Def\Word;
use MC\Parser\DefError;
use Tests\ParserTest;

/**
* Parser-generator class with an easy PHP-based API, similar to the pyparsing module in philosophy.
*
* @see \Tests\ParserTest
* @see ParserTest
*/
class Parser
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public function oneOf(Def ...$args): OneOf
/**
* Return a Word that matches a set of possible characters not separated by whitespace.
*/
public function word(string $firstChars, string $restChars = null): Word
public function word(string $firstChars, ?string $restChars = null): Word
{
return new Word($firstChars, $restChars);
}
Expand Down
6 changes: 1 addition & 5 deletions lib/MC/Parser/Def.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ public function parsePart(string $str, int $loc): array
{
[$loc, $tok] = $this->_parse($str, $loc);

$char = @$str[$loc++];
while (('' !== $char) && Parser::isWhitespace($char)) {
$char = @$str[$loc++];
}
--$loc;
for (; ($loc < strlen($str)) && Parser::isWhitespace($str[$loc]); ++$loc);

return [$loc, $tok];
}
Expand Down
2 changes: 1 addition & 1 deletion lib/MC/Parser/Def/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Regex extends Def
/** @var int */
public $retgroup = 0;

public function __construct(string $regex = null, string $flags = null, string $errstr = null)
public function __construct(?string $regex = null, ?string $flags = null, ?string $errstr = null)
{
if (null !== $regex) {
$this->regex = $regex;
Expand Down
2 changes: 1 addition & 1 deletion lib/MC/Parser/Def/Word.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Word extends Regex
* @param string $firstChars the characters allowed as the first character in the word
* @param null|string $restChars the characters allowed as the rest of the word - defaults to same as $first_chars
*/
public function __construct(string $firstChars, string $restChars = null)
public function __construct(string $firstChars, ?string $restChars = null)
{
parent::__construct();

Expand Down
4 changes: 1 addition & 3 deletions lib/MC/Parser/DefError.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace MC\Parser;

class DefError extends Error
{
}
class DefError extends Error {}
4 changes: 1 addition & 3 deletions lib/MC/Parser/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use Exception;

class Error extends Exception
{
}
class Error extends Exception {}
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
parameters:
checkMissingIterableValueType: false
reportUnmatchedIgnoredErrors: false
reportUnmatchedIgnoredErrors: true
treatPhpDocTypesAsCertain: false
4 changes: 4 additions & 0 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public function testQueryJoins(): void

$output = $vis->handleRequest(false, $parameters);

// hack, different PHP version have different float rounding
$output = preg_replace('/69\.22149730656[0-9]+/', '69.22149730656', $output);
assert(is_string($output));

// file_put_contents(__DIR__.'/result3.js', $output);
self::assertStringEqualsFile(__DIR__.'/result3.js', $output);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/result3.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.