Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $db = new PDO('mysql:dbname=test;host=localhost', 'root', 'myself');
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $documentedAs = array(
- 'ATTR_AUTOCOMMIT' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_PREFETCH' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => 'true',
- ),
- 'ATTR_TIMEOUT' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_ERRMODE' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_SERVER_VERSION' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_CLIENT_VERSION' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_SERVER_INFO' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_CONNECTION_STATUS' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_CASE' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_CURSOR_NAME' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_CURSOR' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_DRIVER_NAME' => array(
- 'type' => 'string',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_ORACLE_NULLS' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_PERSISTENT' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_STATEMENT_CLASS' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_FETCH_CATALOG_NAMES' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => true,
- ),
- 'ATTR_FETCH_TABLE_NAMES' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => true,
- ),
- 'ATTR_STRINGIFY_FETCHES' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_MAX_COLUMN_LEN' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_DEFAULT_FETCH_MODE' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'ATTR_EMULATE_PREPARES' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_USE_BUFFERED_QUERY' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_LOCAL_INFILE' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_INIT_COMMAND' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_READ_DEFAULT_FILE' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => true,
- ),
- 'MYSQL_ATTR_READ_DEFAULT_GROUP' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => true,
- ),
- 'MYSQL_ATTR_MAX_BUFFER_SIZE' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => true,
- ),
- 'MYSQL_ATTR_DIRECT_QUERY' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_FOUND_ROWS' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_IGNORE_SPACE' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_COMPRESS' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => true,
- ),
- 'MYSQL_ATTR_SSL_CA' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_SSL_CAPATH' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_SSL_CERT' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_CIPHER' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- 'MYSQL_ATTR_KEY' => array(
- 'type' => 'integer',
- 'notAlwaysPresent' => false,
- ),
- );
- $reflection = new ReflectionClass($db);
- function writeNotice($str)
- {
- static $nr = 1;
- echo "#{$nr} {$str}" . PHP_EOL;
- $nr++;
- }
- // documented but not defined as class constant:
- foreach (array_diff(array_keys($documentedAs), array_keys($reflection->getConstants())) as $documentedButNotExisting) {
- if (!$documentedAs[$documentedButNotExisting]['notAlwaysPresent']) {
- writeNotice("{$documentedButNotExisting} is in the documentation but not defined as a class constant.");
- }
- }
- foreach ($reflection->getConstants() as $constantName => $constant) {
- if (strpos($constantName, 'ATTR') !== false) {
- // defined but not documented:
- if (!isset($documentedAs[$constantName])) {
- writeNotice("{$constantName} is defined as a class constant but is not in the documentation.");
- continue;
- }
- try {
- $attrValue = $db->getAttribute($constant);
- // documented but with wrong type
- $actualType = gettype($attrValue);
- $documentedType = $documentedAs[$constantName]['type'];
- if ($actualType !== $documentedType) {
- writeNotice("{$constantName} has type {$actualType} but is documented as {$documentedType}.");
- }
- }
- catch (PDOException $e) {
- // documented, defined as class constant but not retrievable with getAttribute()
- if (!$documentedAs[$constantName]['notAlwaysPresent']) {
- writeNotice("{$constantName} could not be retrieved but the documentation makes it appear it is always present.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement