Skip to content
Permalink
Browse files

Fix #1542 - disable SQL parsing by default

  • Loading branch information...
muglug committed Apr 22, 2019
1 parent cd74e22 commit d1f4622a80009a5beb3dc0706b850633ae57febd
Showing with 46 additions and 3 deletions.
  1. +20 −0 src/Psalm/Config.php
  2. +5 −3 src/Psalm/Type.php
  3. +21 −0 tests/ValueTest.php
@@ -375,6 +375,16 @@ class Config
*/
public $globals = [];
/**
* @var bool
*/
public $parse_sql = false;
/**
* @var int
*/
public $max_string_length = 1000;
protected function __construct()
{
self::$instance = $this;
@@ -686,6 +696,16 @@ public static function loadFromXML($base_dir, $file_contents)
$config->error_baseline = $attribute_text;
}
if (isset($config_xml['maxStringLength'])) {
$attribute_text = intval($config_xml['maxStringLength']);
$config->max_string_length = $attribute_text;
}
if (isset($config_xml['parseSql'])) {
$attribute_text = (string) $config_xml['parseSql'];
$config->parse_sql = $attribute_text === 'true' || $attribute_text === '1';
}
if (isset($config_xml->projectFiles)) {
$config->project_files = ProjectFileFilter::loadFromXMLElement($config_xml->projectFiles, $base_dir, true);
}
@@ -931,21 +931,23 @@ public static function getString($value = null)
$type = null;
if ($value !== null) {
if (stripos($value, 'select ') !== false) {
$config = \Psalm\Config::getInstance();
if ($config->parse_sql && stripos($value, 'select ') !== false) {
try {
$parser = new \PhpMyAdmin\SqlParser\Parser($value);
if (!$parser->errors) {
$type = new TSqlSelectString($value);
}
} catch (\Throwable $e) {
if (strlen($value) < 1000) {
if (strlen($value) < $config->max_string_length) {
$type = new TLiteralString($value);
}
}
}
if (!$type && strlen($value) < 1000) {
if (!$type && strlen($value) < $config->max_string_length) {
$type = new TLiteralString($value);
}
}
@@ -6,6 +6,27 @@ class ValueTest extends TestCase
use Traits\InvalidCodeAnalysisTestTrait;
use Traits\ValidCodeAnalysisTestTrait;
/**
* @return void
*/
public function setUp()
{
\Psalm\Internal\Analyzer\FileAnalyzer::clearCache();
$this->file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider();
$this->project_analyzer = new \Psalm\Internal\Analyzer\ProjectAnalyzer(
new TestConfig(),
new \Psalm\Internal\Provider\Providers(
$this->file_provider,
new \Psalm\Tests\Internal\Provider\FakeParserCacheProvider()
)
);
$this->project_analyzer->setPhpVersion('7.3');
$this->project_analyzer->getCodebase()->config->parse_sql = true;
}
/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/

0 comments on commit d1f4622

Please sign in to comment.
You can’t perform that action at this time.