PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Symfony/CS/Fixer/Psr0Fixer.php

https://github.com/hlubek/PHP-CS-Fixer
PHP | 141 lines | 105 code | 23 blank | 13 comment | 19 complexity | 99c39366b15c6ccbb1f200f5ba075f2f MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Fixer;
  11. use Symfony\CS\ConfigInterface;
  12. use Symfony\CS\ConfigAwareInterface;
  13. use Symfony\CS\FixerInterface;
  14. /**
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class Psr0Fixer implements FixerInterface, ConfigAwareInterface
  18. {
  19. protected $config;
  20. public function fix(\SplFileInfo $file, $content)
  21. {
  22. $namespace = false;
  23. if (preg_match('{^[^\S\n]*(?:<\?php\s+)?namespace\s+(\S+)\s*;}um', $content, $match)) {
  24. $namespace = $match[1];
  25. if (stripos($match[0], 'namespace') > 0) {
  26. $content = str_replace($match[0], ltrim($match[0], " \t"), $content);
  27. }
  28. }
  29. if (!preg_match_all('{^((abstract\s+|final\s+)?class|interface|trait)\s+(\S+)}um', $content, $matches, PREG_SET_ORDER)) {
  30. return $content;
  31. }
  32. // no classes?
  33. if (!$matches) {
  34. return $content;
  35. }
  36. if (count($matches) > 1) {
  37. return $content;
  38. }
  39. $match = $matches[0];
  40. $keyword = $match[1];
  41. $class = $match[3];
  42. if ($namespace) {
  43. $normNamespace = strtr($namespace, '\\', '/');
  44. $path = strtr($file->getRealPath(), '\\', '/');
  45. $dir = dirname($path);
  46. $filename = basename($path, '.php');
  47. if ($this->config) {
  48. $dir = substr($dir, strlen(realpath($this->config->getDir())) + 1);
  49. if (strlen($normNamespace) > strlen($dir)) {
  50. if (strlen($dir)) {
  51. $normNamespace = substr($normNamespace, -strlen($dir));
  52. } else {
  53. $normNamespace = '';
  54. }
  55. }
  56. }
  57. $dir = substr($dir, -strlen($normNamespace));
  58. if (false === $dir) {
  59. $dir = '';
  60. }
  61. $filename = basename($path, '.php');
  62. if ($class !== $filename) {
  63. $content = preg_replace('{^'.$keyword.'\s+(\S+)}um', $keyword.' '.$filename, $content, 1);
  64. }
  65. if ($normNamespace !== $dir) {
  66. if (strtolower($normNamespace) === strtolower($dir)) {
  67. $namespace = substr($namespace, 0, -strlen($dir)) . strtr($dir, '/', '\\');
  68. $content = preg_replace('{^namespace\s+(\S+)\s*;}um', 'namespace '.$namespace.';', $content, 1);
  69. } else {
  70. echo '! The namespace '.$namespace.' in '.$path.' does not match the file path according to PSR-0 rules'.PHP_EOL;
  71. }
  72. }
  73. } else {
  74. $normClass = strtr($class, '_', '/');
  75. $path = strtr($file->getRealPath(), '\\', '/');
  76. $filename = substr($path, -strlen($normClass)-4, -4);
  77. if (!strpos($class, '_')) {
  78. echo '! Class '.$class.' in '.$path.' should have at least a vendor namespace according to PSR-0 rules'.PHP_EOL;
  79. }
  80. if ($normClass !== $filename) {
  81. if (strtolower($normClass) === strtolower($filename)) {
  82. $content = preg_replace('{^'.$keyword.'\s+(\S+)}um', $keyword.' '.strtr($filename, '/', '_'), $content, 1);
  83. } else {
  84. echo '! The class '.$class.' in '.$path.' does not match the file path according to PSR-0 rules'.PHP_EOL;
  85. }
  86. }
  87. }
  88. return $content;
  89. }
  90. public function setConfig(ConfigInterface $config)
  91. {
  92. $this->config = $config;
  93. }
  94. public function getLevel()
  95. {
  96. return FixerInterface::PSR0_LEVEL;
  97. }
  98. public function getPriority()
  99. {
  100. return -10;
  101. }
  102. public function supports(\SplFileInfo $file)
  103. {
  104. if ('php' !== pathinfo($file->getFilename(), PATHINFO_EXTENSION)) {
  105. return false;
  106. }
  107. // ignore stubs/fixtures, since they are typically containing invalid files for various reasons
  108. return !preg_match('{[/\\\\](stub|fixture)s?[/\\\\]}i', $file->getRealPath());
  109. }
  110. public function getName()
  111. {
  112. return 'psr0';
  113. }
  114. public function getDescription()
  115. {
  116. return 'Classes must be in a path that matches their namespace, be at least one namespace deep, and the class name should match the file name.';
  117. }
  118. }