for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace vipnytt\RobotsTxtParser\SQL;
use PDO;
use PDOException;
/**
* Class SQLTrait
*
* @package vipnytt\RobotsTxtParser\SQL
*/
trait SQLTrait
{
* Initialize PDO connection
* @param PDO $pdo |null
* @return PDO
private function pdoInitialize(PDO $pdo = null)
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$pdo
null
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:
function someFunction(A $objectMaybe = null) { if ($objectMaybe instanceof A) { $objectMaybe->doSomething(); } }
$pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
$pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
return $pdo;
}
* Create table
* @param PDO $pdo
* @param string $table
* @param string $query
* @return bool
private function createTable(PDO $pdo, $table, $query)
if ($this->tableExists($pdo, $table)) {
return true;
try {
$pdo->query($query);
} catch (PDOException $e) {
return FALSE;
return $this->tableExists($pdo, $table);
* Check if the table exists
private function tableExists(PDO $pdo, $table)
$result = $pdo->query("SELECT 1 FROM $table LIMIT 1;");
} catch (\Exception $e) {
return $result !== FALSE;
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: