Skip to content

Commit dec8435

Browse files
committed
snapshot of dev workspace
1 parent eb40b31 commit dec8435

File tree

6 files changed

+360
-2
lines changed

6 files changed

+360
-2
lines changed
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Applied Object Calisthenics for Symfony 2
5+
*
6+
* PHP version 5
7+
*
8+
* @category PHP
9+
* @package PHP_CodeSniffer-Symfony2-ObjectCalisthenics
10+
* @license http://spdx.org/licenses/MIT MIT License
11+
* @version GIT: master
12+
* @link https://github.com/instaclick/Symfony2-coding-standard
13+
*/
14+
15+
/**
16+
* Symfony2_Sniffs_ObjectCalisthenics_DumpSniff.
17+
*
18+
* Debug/dump AST
19+
*
20+
* @category PHP
21+
* @package PHP_CodeSniffer-Symfony2-ObjectCalisthenics
22+
* @author Anthon Pang <[email protected]>
23+
* @license http://spdx.org/licenses/MIT MIT License
24+
* @link https://github.com/instaclick/Symfony2-coding-standard
25+
*/
26+
class Symfony2_Sniffs_ObjectCalisthenics_DumpSniff implements PHP_CodeSniffer_Sniff
27+
{
28+
/**
29+
* A list of tokenizers this sniff supports.
30+
*
31+
* @var array
32+
*/
33+
public $supportedTokenizers = array(
34+
'PHP',
35+
);
36+
37+
/**
38+
* Returns an array of tokens this test wants to listen for.
39+
*
40+
* @return array
41+
*/
42+
public function register()
43+
{
44+
return array(T_OPEN_TAG);
45+
}
46+
47+
/**
48+
* Processes this test, when one of its tokens is encountered.
49+
*
50+
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
51+
* @param int $stackPtr The position of the current token in
52+
* the stack passed in $tokens.
53+
*/
54+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
55+
{
56+
return;
57+
58+
$parser = new PHPParser_Parser(new PHPParser_Lexer);
59+
60+
$visitor = new Symfony2_Sniffs_ObjectCalisthenics_UseAccessorsSniff_NodeVisitor;
61+
$visitor->setPHPCodeSnifferFile($phpcsFile);
62+
63+
$traverser = new PHPParser_NodeTraverser;
64+
$traverser->addVisitor($visitor);
65+
66+
try {
67+
$code = file_get_contents($phpcsFile->getFilename());
68+
$tree = $parser->parse($code);
69+
//$tree[0]->setAttribute('visited', true);
70+
var_dump($tree);
71+
} catch (PHPParser_Error $e) {
72+
}
73+
die;
74+
}
75+
}
76+
// 3 - wrap primitive types and string, if it has behavior
77+
// 4 - only one -> per line, if not getter or fluent
78+
// 5 - don't abbreviate
79+
// 7 - limit the number of instance variables in a class to 5
80+
// 8 - use first class collections
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Applied Object Calisthenics for Symfony 2
5+
*
6+
* PHP version 5
7+
*
8+
* @category PHP
9+
* @package PHP_CodeSniffer-Symfony2-ObjectCalisthenics
10+
* @license http://spdx.org/licenses/MIT MIT License
11+
* @version GIT: master
12+
* @link https://github.com/instaclick/Symfony2-coding-standard
13+
*/
14+
15+
/**
16+
* Symfony2_Sniffs_ObjectCalisthenics_OneDotPerLineSniff.
17+
*
18+
* Only one -> per line, if not getter or fluent.
19+
*
20+
* @category PHP
21+
* @package PHP_CodeSniffer-Symfony2-ObjectCalisthenics
22+
* @author Anthon Pang <[email protected]>
23+
* @license http://spdx.org/licenses/MIT MIT License
24+
* @link https://github.com/instaclick/Symfony2-coding-standard
25+
*/
26+
class Symfony2_Sniffs_ObjectCalisthenics_OneDotPerLineSniff implements PHP_CodeSniffer_Sniff
27+
{
28+
/**
29+
* A list of tokenizers this sniff supports.
30+
*
31+
* @var array
32+
*/
33+
public $supportedTokenizers = array(
34+
'PHP',
35+
);
36+
37+
/**
38+
* Returns an array of tokens this test wants to listen for.
39+
*
40+
* @return array
41+
*/
42+
public function register()
43+
{
44+
return array(T_OPEN_TAG);
45+
}
46+
47+
/**
48+
* Processes this test, when one of its tokens is encountered.
49+
*
50+
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
51+
* @param int $stackPtr The position of the current token in
52+
* the stack passed in $tokens.
53+
*/
54+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
55+
{
56+
if ($stackPtr !== 0) {
57+
return;
58+
}
59+
60+
$parser = new PHPParser_Parser(new PHPParser_Lexer);
61+
62+
$visitor = new Symfony2_Sniffs_ObjectCalisthenics_OneDotPerLineSniff_NodeVisitor;
63+
$visitor->setPHPCodeSnifferFile($phpcsFile);
64+
65+
$traverser = new PHPParser_NodeTraverser;
66+
$traverser->addVisitor($visitor);
67+
68+
try {
69+
$code = file_get_contents($phpcsFile->getFilename());
70+
$tree = $parser->parse($code);
71+
$tree = $traverser->traverse($tree);
72+
} catch (PHPParser_Error $e) {
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Parse tree visitor for control structures
4+
*
5+
* @author Anthon Pang <[email protected]>
6+
*/
7+
class Symfony2_Sniffs_ObjectCalisthenics_OneDotPerLineSniff_NodeVisitor extends PHPParser_NodeVisitorAbstract
8+
{
9+
/**
10+
* @var PHP_CodeSniffer_File
11+
*/
12+
private $phpcsFile;
13+
14+
/**
15+
* Set PHP CodeSniffer File
16+
*
17+
* @param PHP_CodeSniffer_File $phpcsFile
18+
*/
19+
public function setPHPCodeSnifferFile(PHP_CodeSniffer_File $phpcsFile)
20+
{
21+
$this->phpcsFile = $phpcsFile;
22+
}
23+
24+
/**
25+
* Search token stream for the corresponding node
26+
*
27+
* @param PHPParser_Node $node Current node
28+
*
29+
* @return integer
30+
*/
31+
private function findStackPointer($node)
32+
{
33+
$tokens = $this->phpcsFile
34+
->getTokens();
35+
36+
foreach ($tokens as $stackPtr => $token) {
37+
if ($node->getLine() > $token['line']) {
38+
continue;
39+
}
40+
41+
return $stackPtr;
42+
}
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function enterNode(PHPParser_Node $node)
49+
{
50+
if ($node->getAttribute('visited')) {
51+
return;
52+
}
53+
return;
54+
switch ($node->getType()) {
55+
case 'Stmt_ClassMethod':
56+
case 'Expr_Closure':
57+
case 'Stmt_Function':
58+
case 'Stmt_If':
59+
case 'Stmt_Do':
60+
case 'Stmt_While':
61+
case 'Stmt_For':
62+
case 'Stmt_Foreach':
63+
break;
64+
}
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function leaveNode(PHPParser_Node $node)
71+
{
72+
return;
73+
switch ($node->getType()) {
74+
case 'Stmt_ClassMethod':
75+
case 'Expr_Closure':
76+
case 'Stmt_Function':
77+
case 'Stmt_If':
78+
case 'Stmt_Do':
79+
case 'Stmt_While':
80+
case 'Stmt_For':
81+
case 'Stmt_Foreach':
82+
break;
83+
}
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* One Dot Per Line Sniff, if not getter or fluent
4+
*/
5+
class OneDotPerLineSniffTest
6+
{
7+
public function validExamples()
8+
{
9+
$this->container;
10+
11+
$this->container
12+
->get('service');
13+
14+
$this->getContainer()
15+
->get('service');
16+
17+
$this->getContainer()->get('service');
18+
19+
self::$container;
20+
21+
self::$container->get('service');
22+
23+
self::$container->get('service')
24+
->getName();
25+
26+
self::$container->get('service')->getName();
27+
28+
$mock = $this->getMockBuilder('FooBar')
29+
->disableOriginalConstructor()
30+
->getMock();
31+
32+
$mock->expects($this->any())
33+
->method('foo')
34+
->will($this->returnValue($mock));
35+
}
36+
37+
public function invalidExamples()
38+
{
39+
// properties break the method chain
40+
$this->container->get('service');
41+
42+
$this->getContainer()->name;
43+
44+
$this->getService()->gettable->getType();
45+
46+
// mixing getters and non-getters in the fluent interface
47+
$mock = $this->getMockBuilder('FooBar')->disableOriginalConstructor()
48+
->getMock();
49+
50+
$mock = $this->getMockBuilder('FooBar')->disableOriginalConstructor()->getMock();
51+
52+
$mock = $this->getMockBuilder('FooBar')
53+
->disableOriginalConstructor()->getMock();
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* This file is part of the Symfony2-coding-standard (phpcs standard)
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer-Symfony2
9+
* @author Symfony2-phpcs-authors <[email protected]>
10+
* @license http://spdx.org/licenses/MIT MIT License
11+
* @version GIT: master
12+
* @link https://github.com/opensky/Symfony2-coding-standard
13+
*/
14+
15+
/**
16+
* Unit test class for the OneDotPerLine sniff.
17+
*
18+
* A sniff unit test checks a .inc file for expected violations of a single
19+
* coding standard. Expected errors and warnings are stored in this class.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Tom Klingenberg <[email protected]>
24+
* @copyright 2012 Tom Klingenberg, some rights reserved.
25+
* @license http://spdx.org/licenses/MIT MIT License
26+
* @link https://github.com/opensky/Symfony2-coding-standard
27+
*/
28+
class Symfony2_Tests_ObjectCalisthenics_OneDotPerLineUnitTest extends AbstractSniffUnitTest
29+
{
30+
/**
31+
* Returns the lines where errors should occur.
32+
*
33+
* The key of the array should represent the line number and the value
34+
* should represent the number of errors that should occur on that line.
35+
*
36+
* @return array(int => int)
37+
*/
38+
public function getErrorList()
39+
{
40+
return array(
41+
40 => 1,
42+
42 => 1,
43+
44 => 1,
44+
47 => 1,
45+
50 => 1,
46+
53 => 1,
47+
);
48+
}
49+
50+
/**
51+
* Returns the lines where warnings should occur.
52+
*
53+
* The key of the array should represent the line number and the value
54+
* should represent the number of warnings that should occur on that line.
55+
*
56+
* @return array(int => int)
57+
*/
58+
public function getWarningList()
59+
{
60+
return array(
61+
);
62+
}
63+
}

ruleset.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@
8888
</rule>
8989

9090
<!-- Disable these object calisthenic sniffs -->
91-
<rule ref="Symfony2.ObjectCalisthenics.SmallClass">
91+
<!--rule ref="Symfony2.ObjectCalisthenics.SmallClass">
9292
<severity>0</severity>
9393
</rule>
9494
9595
<rule ref="Symfony2.ObjectCalisthenics.UseAccessors">
9696
<severity>0</severity>
97-
</rule>
97+
</rule-->
9898
</ruleset>

0 commit comments

Comments
 (0)