/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Generic/Sniffs/VersionControl/SubversionPropertiesSniff.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 206 lines · 102 code · 33 blank · 71 comment · 15 complexity · a2ce399914cb0be774c1345f0217256a MD5 · raw file

  1. <?php
  2. /**
  3. * Generic_Sniffs_VersionControl_SubversionPropertiesSniff.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Jack Bates <ms419@freezone.co.uk>
  10. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  11. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12. * @link http://pear.php.net/package/PHP_CodeSniffer
  13. */
  14. /**
  15. * Generic_Sniffs_VersionControl_SubversionPropertiesSniff.
  16. *
  17. * Tests that the correct Subversion properties are set.
  18. *
  19. * @category PHP
  20. * @package PHP_CodeSniffer
  21. * @author Jack Bates <ms419@freezone.co.uk>
  22. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  23. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  24. * @version Release: @package_version@
  25. * @link http://pear.php.net/package/PHP_CodeSniffer
  26. */
  27. class Generic_Sniffs_VersionControl_SubversionPropertiesSniff implements PHP_CodeSniffer_Sniff
  28. {
  29. /**
  30. * The Subversion properties that should be set.
  31. *
  32. * Key of array is the SVN property and the value is the
  33. * exact value the property should have or NULL if the
  34. * property should just be set but the value is not fixed.
  35. *
  36. * @var array
  37. */
  38. protected $properties = array(
  39. 'svn:keywords' => 'Author Id Revision',
  40. 'svn:eol-style' => 'native',
  41. );
  42. /**
  43. * Returns an array of tokens this test wants to listen for.
  44. *
  45. * @return array
  46. */
  47. public function register()
  48. {
  49. return array(T_OPEN_TAG);
  50. }//end register()
  51. /**
  52. * Processes this test, when one of its tokens is encountered.
  53. *
  54. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  55. * @param int $stackPtr The position of the current token
  56. * in the stack passed in $tokens.
  57. *
  58. * @return void
  59. */
  60. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  61. {
  62. $tokens = $phpcsFile->getTokens();
  63. // Make sure this is the first PHP open tag so we don't process the
  64. // same file twice.
  65. $prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1));
  66. if ($prevOpenTag !== false) {
  67. return;
  68. }
  69. $path = $phpcsFile->getFileName();
  70. $properties = $this->getProperties($path);
  71. if ($properties === null) {
  72. // Not under version control.
  73. return;
  74. }
  75. $allProperties = $properties + $this->properties;
  76. foreach ($allProperties as $key => $value) {
  77. if (isset($properties[$key]) === true
  78. && isset($this->properties[$key]) === false
  79. ) {
  80. $error = 'Unexpected Subversion property "%s" = "%s"';
  81. $data = array(
  82. $key,
  83. $properties[$key],
  84. );
  85. $phpcsFile->addError($error, $stackPtr, 'Unexpected', $data);
  86. continue;
  87. }
  88. if (isset($properties[$key]) === false
  89. && isset($this->properties[$key]) === true
  90. ) {
  91. $error = 'Missing Subversion property "%s" = "%s"';
  92. $data = array(
  93. $key,
  94. $this->properties[$key],
  95. );
  96. $phpcsFile->addError($error, $stackPtr, 'Missing', $data);
  97. continue;
  98. }
  99. if ($properties[$key] !== null
  100. && $properties[$key] !== $this->properties[$key]
  101. ) {
  102. $error = 'Subversion property "%s" = "%s" does not match "%s"';
  103. $data = array(
  104. $key,
  105. $properties[$key],
  106. $this->properties[$key],
  107. );
  108. $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
  109. }
  110. }//end foreach
  111. }//end process()
  112. /**
  113. * Returns the Subversion properties which are actually set on a path.
  114. *
  115. * Returns NULL if the file is not under version control.
  116. *
  117. * @param string $path The path to return Subversion properties on.
  118. *
  119. * @return array
  120. * @throws PHP_CodeSniffer_Exception If Subversion properties file could
  121. * not be opened.
  122. */
  123. protected function getProperties($path)
  124. {
  125. $properties = array();
  126. $paths = array();
  127. $paths[] = dirname($path).'/.svn/props/'.basename($path).'.svn-work';
  128. $paths[] = dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base';
  129. $foundPath = false;
  130. foreach ($paths as $path) {
  131. if (file_exists($path) === true) {
  132. $foundPath = true;
  133. $handle = fopen($path, 'r');
  134. if ($handle === false) {
  135. $error = 'Error opening file; could not get Subversion properties';
  136. throw new PHP_CodeSniffer_Exception($error);
  137. }
  138. while (feof($handle) === false) {
  139. // Read a key length line. Might be END, though.
  140. $buffer = trim(fgets($handle));
  141. // Check for the end of the hash.
  142. if ($buffer === 'END') {
  143. break;
  144. }
  145. // Now read that much into a buffer.
  146. $key = fread($handle, substr($buffer, 2));
  147. // Suck up extra newline after key data.
  148. fgetc($handle);
  149. // Read a value length line.
  150. $buffer = trim(fgets($handle));
  151. // Now read that much into a buffer.
  152. $length = substr($buffer, 2);
  153. if ($length === '0') {
  154. // Length of value is ZERO characters, so
  155. // value is actually empty.
  156. $value = '';
  157. } else {
  158. $value = fread($handle, $length);
  159. }
  160. // Suck up extra newline after value data.
  161. fgetc($handle);
  162. $properties[$key] = $value;
  163. }//end while
  164. fclose($handle);
  165. }//end if
  166. }//end foreach
  167. if ($foundPath === false) {
  168. return null;
  169. }
  170. return $properties;
  171. }//end getProperties()
  172. }//end class
  173. ?>