Advertisement
Guest User

Check PHP PDO attribute behavior against documented behavior

a guest
Dec 15th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.78 KB | None | 0 0
  1. <?php
  2.  
  3. $db = new PDO('mysql:dbname=test;host=localhost', 'root', 'myself');
  4.  
  5. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  6.  
  7. $documentedAs = array(
  8.     'ATTR_AUTOCOMMIT' => array(
  9.         'type' => 'integer',
  10.         'notAlwaysPresent' => false,
  11.     ),
  12.     'ATTR_PREFETCH' => array(
  13.         'type' => 'integer',
  14.         'notAlwaysPresent' => 'true',
  15.     ),
  16.     'ATTR_TIMEOUT' => array(
  17.         'type' => 'integer',
  18.         'notAlwaysPresent' => false,
  19.     ),
  20.     'ATTR_ERRMODE' => array(
  21.         'type' => 'integer',
  22.         'notAlwaysPresent' => false,
  23.     ),
  24.     'ATTR_SERVER_VERSION' => array(
  25.         'type' => 'integer',
  26.         'notAlwaysPresent' => false,
  27.     ),
  28.     'ATTR_CLIENT_VERSION' => array(
  29.         'type' => 'integer',
  30.         'notAlwaysPresent' => false,
  31.     ),
  32.     'ATTR_SERVER_INFO' => array(
  33.         'type' => 'integer',
  34.         'notAlwaysPresent' => false,
  35.     ),
  36.     'ATTR_CONNECTION_STATUS' => array(
  37.         'type' => 'integer',
  38.         'notAlwaysPresent' => false,
  39.     ),
  40.     'ATTR_CASE' => array(
  41.         'type' => 'integer',
  42.         'notAlwaysPresent' => false,
  43.     ),
  44.     'ATTR_CURSOR_NAME' => array(
  45.         'type' => 'integer',
  46.         'notAlwaysPresent' => false,
  47.     ),
  48.     'ATTR_CURSOR' => array(
  49.         'type' => 'integer',
  50.         'notAlwaysPresent' => false,
  51.     ),
  52.     'ATTR_DRIVER_NAME' => array(
  53.         'type' => 'string',
  54.         'notAlwaysPresent' => false,
  55.     ),
  56.     'ATTR_ORACLE_NULLS' => array(
  57.         'type' => 'integer',
  58.         'notAlwaysPresent' => false,
  59.     ),
  60.     'ATTR_PERSISTENT' => array(
  61.         'type' => 'integer',
  62.         'notAlwaysPresent' => false,
  63.     ),
  64.     'ATTR_STATEMENT_CLASS' => array(
  65.         'type' => 'integer',
  66.         'notAlwaysPresent' => false,
  67.     ),
  68.     'ATTR_FETCH_CATALOG_NAMES' => array(
  69.         'type' => 'integer',
  70.         'notAlwaysPresent' => true,
  71.     ),
  72.     'ATTR_FETCH_TABLE_NAMES' => array(
  73.         'type' => 'integer',
  74.         'notAlwaysPresent' => true,
  75.     ),
  76.     'ATTR_STRINGIFY_FETCHES' => array(
  77.         'type' => 'integer',
  78.         'notAlwaysPresent' => false,
  79.     ),
  80.     'ATTR_MAX_COLUMN_LEN' => array(
  81.         'type' => 'integer',
  82.         'notAlwaysPresent' => false,
  83.     ),
  84.     'ATTR_DEFAULT_FETCH_MODE' => array(
  85.         'type' => 'integer',
  86.         'notAlwaysPresent' => false,
  87.     ),
  88.     'ATTR_EMULATE_PREPARES' => array(
  89.         'type' => 'integer',
  90.         'notAlwaysPresent' => false,
  91.     ),
  92.     'MYSQL_ATTR_USE_BUFFERED_QUERY' => array(
  93.         'type' => 'integer',
  94.         'notAlwaysPresent' => false,
  95.     ),
  96.     'MYSQL_ATTR_LOCAL_INFILE' => array(
  97.         'type' => 'integer',
  98.         'notAlwaysPresent' => false,
  99.     ),
  100.     'MYSQL_ATTR_INIT_COMMAND' => array(
  101.         'type' => 'integer',
  102.         'notAlwaysPresent' => false,
  103.     ),
  104.     'MYSQL_ATTR_READ_DEFAULT_FILE' => array(
  105.         'type' => 'integer',
  106.         'notAlwaysPresent' => true,
  107.     ),
  108.     'MYSQL_ATTR_READ_DEFAULT_GROUP' => array(
  109.         'type' => 'integer',
  110.         'notAlwaysPresent' => true,
  111.     ),
  112.     'MYSQL_ATTR_MAX_BUFFER_SIZE' => array(
  113.         'type' => 'integer',
  114.         'notAlwaysPresent' => true,
  115.     ),
  116.     'MYSQL_ATTR_DIRECT_QUERY' => array(
  117.         'type' => 'integer',
  118.         'notAlwaysPresent' => false,
  119.     ),
  120.     'MYSQL_ATTR_FOUND_ROWS' => array(
  121.         'type' => 'integer',
  122.         'notAlwaysPresent' => false,
  123.     ),
  124.     'MYSQL_ATTR_IGNORE_SPACE' => array(
  125.         'type' => 'integer',
  126.         'notAlwaysPresent' => false,
  127.     ),
  128.     'MYSQL_ATTR_COMPRESS' => array(
  129.         'type' => 'integer',
  130.         'notAlwaysPresent' => true,
  131.     ),
  132.     'MYSQL_ATTR_SSL_CA' => array(
  133.         'type' => 'integer',
  134.         'notAlwaysPresent' => false,
  135.     ),
  136.     'MYSQL_ATTR_SSL_CAPATH' => array(
  137.         'type' => 'integer',
  138.         'notAlwaysPresent' => false,
  139.     ),
  140.     'MYSQL_ATTR_SSL_CERT' => array(
  141.         'type' => 'integer',
  142.         'notAlwaysPresent' => false,
  143.     ),
  144.     'MYSQL_ATTR_CIPHER' => array(
  145.         'type' => 'integer',
  146.         'notAlwaysPresent' => false,
  147.     ),
  148.     'MYSQL_ATTR_KEY' => array(
  149.         'type' => 'integer',
  150.         'notAlwaysPresent' => false,
  151.     ),
  152. );
  153.  
  154. $reflection = new ReflectionClass($db);
  155.  
  156. function writeNotice($str)
  157. {
  158.     static $nr = 1;
  159.     echo "#{$nr} {$str}" . PHP_EOL;
  160.     $nr++;
  161. }
  162.  
  163. // documented but not defined as class constant:
  164. foreach (array_diff(array_keys($documentedAs), array_keys($reflection->getConstants())) as $documentedButNotExisting) {
  165.     if (!$documentedAs[$documentedButNotExisting]['notAlwaysPresent']) {
  166.         writeNotice("{$documentedButNotExisting} is in the documentation but not defined as a class constant.");
  167.     }
  168. }
  169.  
  170. foreach ($reflection->getConstants() as $constantName => $constant) {
  171.     if (strpos($constantName, 'ATTR') !== false) {
  172.         // defined but not documented:
  173.         if (!isset($documentedAs[$constantName])) {
  174.             writeNotice("{$constantName} is defined as a class constant but is not in the documentation.");
  175.             continue;
  176.         }
  177.         try {
  178.             $attrValue = $db->getAttribute($constant);
  179.  
  180.             // documented but with wrong type
  181.             $actualType = gettype($attrValue);
  182.             $documentedType = $documentedAs[$constantName]['type'];
  183.             if ($actualType !== $documentedType) {
  184.                 writeNotice("{$constantName} has type {$actualType} but is documented as {$documentedType}.");
  185.             }
  186.         }
  187.         catch (PDOException $e) {
  188.             // documented, defined as class constant but not retrievable with getAttribute()
  189.             if (!$documentedAs[$constantName]['notAlwaysPresent']) {
  190.                 writeNotice("{$constantName} could not be retrieved but the documentation makes it appear it is always present.");
  191.             }
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement