Conditions | 5 |
Paths | 1 |
Total Lines | 87 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
46 | private function loadPhpcrOdm() |
||
47 | { |
||
48 | $this['psi_content_type.storage.doctrine.phpcr_odm.property_encoder'] = function ($container) { |
||
49 | return new PropertyEncoder('psict', 'https://github.com/psiphp/content-type'); |
||
50 | }; |
||
51 | |||
52 | $this['psi_content_type.storage.doctrine.phpcr_odm.field_mapper'] = function ($container) { |
||
53 | return new FieldMapper( |
||
54 | $container['psi_content_type.storage.doctrine.phpcr_odm.property_encoder'], |
||
55 | $container['psi_content_type.field_loader'] |
||
56 | ); |
||
57 | }; |
||
58 | |||
59 | $this['psi_content_type.storage.doctrine.phpcr_odm.collection_updater'] = function ($container) { |
||
60 | return new CollectionIdentifierUpdater( |
||
61 | $container['psi_content_type.metadata.factory'], |
||
62 | $container['psi_content_type.storage.doctrine.phpcr_odm.property_encoder'] |
||
63 | ); |
||
64 | }; |
||
65 | |||
66 | $this['doctrine_phpcr.document_manager'] = function ($container) { |
||
67 | $registerNodeTypes = false; |
||
68 | |||
69 | // automatically setup the schema if the db doesn't exist yet. |
||
70 | if (!file_exists($container['config']['db_path'])) { |
||
71 | if (!file_exists($dir = dirname($container['config']['db_path']))) { |
||
72 | mkdir($dir); |
||
73 | } |
||
74 | |||
75 | $connection = $container['dbal.connection']; |
||
76 | |||
77 | $schema = new RepositorySchema(); |
||
78 | foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) { |
||
79 | $connection->exec($sql); |
||
80 | } |
||
81 | |||
82 | $registerNodeTypes = true; |
||
83 | } |
||
84 | |||
85 | // register the phpcr session |
||
86 | $factory = new RepositoryFactoryDoctrineDBAL(); |
||
87 | $repository = $factory->getRepository([ |
||
88 | 'jackalope.doctrine_dbal_connection' => $container['dbal.connection'], |
||
89 | ]); |
||
90 | $session = $repository->login(new SimpleCredentials(null, null), 'default'); |
||
91 | |||
92 | if ($registerNodeTypes) { |
||
93 | $typeRegistrator = new NodeTypeRegistrator(); |
||
94 | $typeRegistrator->registerNodeTypes($session); |
||
95 | $ctTypeRegistrator = new CtNodeTypeRegistrator( |
||
96 | $container['psi_content_type.storage.doctrine.phpcr_odm.property_encoder'] |
||
97 | ); |
||
98 | $ctTypeRegistrator->registerNodeTypes($session); |
||
99 | } |
||
100 | |||
101 | // annotation driver |
||
102 | $annotationDriver = new AnnotationDriver($container['annotation_reader'], [ |
||
103 | __DIR__ . '/../../vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Document', |
||
104 | __DIR__ . '/Example', |
||
105 | ]); |
||
106 | $xmlDriver = new XmlDriver([__DIR__ . '/mappings']); |
||
107 | $annotationDriver = new AnnotationDriver($container['annotation_reader'], [ |
||
108 | __DIR__ . '/../../vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Document', |
||
109 | __DIR__ . '/Example', |
||
110 | ]); |
||
111 | $chain = new MappingDriverChain(); |
||
112 | $chain->addDriver($annotationDriver, 'Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example'); |
||
113 | $chain->addDriver($xmlDriver, 'Psi\Component\ContentType\Tests\Functional\Example\Model'); |
||
114 | $chain->addDriver($annotationDriver, 'Doctrine'); |
||
115 | |||
116 | $config = new Configuration(); |
||
117 | $config->setMetadataDriverImpl($chain); |
||
118 | |||
119 | $manager = DocumentManager::create($session, $config); |
||
120 | $manager->getEventManager()->addEventSubscriber(new MetadataSubscriber( |
||
121 | $container['psi_content_type.metadata.factory'], |
||
122 | $container['psi_content_type.field_loader'], |
||
123 | $container['psi_content_type.storage.doctrine.phpcr_odm.field_mapper'] |
||
124 | )); |
||
125 | $manager->getEventManager()->addEventSubscriber(new CollectionSubscriber( |
||
126 | $container['psi_content_type.metadata.factory'], |
||
127 | $container['psi_content_type.storage.doctrine.phpcr_odm.property_encoder'] |
||
128 | )); |
||
129 | |||
130 | return $manager; |
||
131 | }; |
||
132 | } |
||
133 | } |
||
134 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: