Problem/Motivation

Webform Analysis should follow Drupal's Coding Standards because it will make it easier for people understand the code and contribute patches.

Proposed resolution

Review and follow Coding Standards

Here is the automated code review.

https://pareview.sh/node/3040

Some suggestions...

  • When in doubt look at core.
  • Add API comments to everything
  • Add \Drupal\webform_analysis\WebformAnalysisInterface and code to Interfaces
  • Use Dependency injection when possible.
  • Always use the webform_analysis namespace, you should change 'webform-admin.css' to 'webform_analysis.admin.css'
  • Instead of using \Drupal\webform_analysis\Form\WebformAnalysisForm::getWebformIdFromRoute use \Drupal\webform\WebformRequest to get the current pages webform entity.
  • Remove \Drupal\webform_analysis\WebformAnalysis::MODULE_NAME and just use the module name.
  • \Drupal\webform_analysis\WebformAnalysis::__construct should be used for dependency injection

You might need to 'cast' the webform to an entity in your routes...

entity.webform.results_analysis:
  path: '/admin/structure/webform/manage/{webform}/results/analysis'
  defaults:
    _form: '\Drupal\webform_analysis\Form\WebformAnalysisForm'
    _title_callback: '\Drupal\webform_analysis\Form\WebformAnalysisForm::getTitle'
  options:
    parameters:
      webform:
        type: 'entity:webform'
  requirements:
    _permission: 'view any webform submission'
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jrockowitz created an issue. See original summary.

  • lbaran committed 6c08b7e on 8.x-1.x
    Issue #2925863: Review and follow Coding Standards
    

  • lbaran committed e3dd6e5 on 8.x-1.x
    Issue #2925863: Review and follow Coding Standards
    

  • lbaran committed 6df2529 on 8.x-1.x
    Issue #2925863: Review and follow Coding Standards
    

  • lbaran committed 37aeb9f on 8.x-1.x
    Issue #2925863: Review and follow Coding Standards
    

  • lbaran committed 0714bad on 8.x-1.x
    Issue #2925863: Review and follow Coding Standards
    
jrockowitz’s picture

Okay, it seems very clear that you want to create an awesome module, so I will keep reviewing and creating tickets.

jrockowitz’s picture

@lbaran If you have questions or need me to contribute a patch to point you in the right direction, just ask.

  • lbaran committed 1d0c058 on 8.x-1.x
    Issue #2925863: Review and follow Coding Standards
    

  • lbaran committed 613594a on 8.x-1.x
    Issue #2925863: Review and follow Coding Standards
    
lbaran’s picture

@jrockowitz, I'll do that. Thank you for your help, support and advice.

  • lbaran committed 35489c7 on 8.x-1.x
    Issue #2925863: Review and follow Coding Standards
    
lbaran’s picture

Status: Active » Fixed

Hello @jrockowitz,

I simplified my code, I use a hook_entity_type_build to add a form handler on Webform.
The route looks like this :

entity.webform.results_analysis:
  path: '/admin/structure/webform/manage/{webform}/results/analysis'
  defaults:
    _entity_form: webform.analysis
  requirements:
    _entity_access: webform.submission_view_any
    _custom_access: '\Drupal\webform\Access\WebformAccess:checkResultsAccess'
  • The WebformAnalysisForm class now extends EntityForm.
  • The WebformAnalysis class set but does not save webform.
  • Webform is saved after the form is submitted with EntityForm::save.

Tell me what you think.

Sincerely,

jrockowitz’s picture

Status: Fixed » Needs review

A few more things related to code standards and patterns (that I missed)...

To prevent namespacing issues...

  • webform-charts.js should be changed to webform_analysis.charts.js
  • Drupal.behaviors.WebformCharts should be changed to Drupal.behaviors.WebformAnalysisCharts

Personally, I would merge \Drupal\webform_analysis\WebformAnalysis into \Drupal\webform_analysis\Form\WebformAnalysisForm which should make the code easier to understand. If the code was to get more complex then I would create an injected service for certain aspects, like rendering, data storage, etc... For example, if the rendering of the charts was an injected service someone could easily override it.

  • lbaran committed 97b8a48 on 8.x-1.x
    Issue #2925863: Review and follow Coding Standards
    

  • lbaran committed 2721b6f on 8.x-1.x
    Issue #2925863 by lbaran: Review and follow Coding Standards
    
anabpv’s picture

Assigned: Unassigned » anabpv

I will review this

anabpv’s picture

Assigned: anabpv » Unassigned

Updated the files with the help of phpcs and phpcbf, mainly:
- Added a new line at the end of the file webform_analysis.links.task.yml
- Added the missing member variable doc comments on several files
- Splitted the array up over multiple lines in file WebformAnalysisBlock.php

Overall, the affected files are:
- webform_analysis.links.task.yml
- WebformAnalysisBlock.php
- WebformAnalysis.php
- WebformAnalysisChart.php
- WebformAnalysisForm.php

Waiting for review.

tmaiochi’s picture

Assigned: Unassigned » tmaiochi

I'll review this!

tmaiochi’s picture

Assigned: tmaiochi » Unassigned
Status: Needs review » Reviewed & tested by the community

The code fixed all messages from PHPCS, however I found this message when I ran phpcs

FILE: /web/modules/contrib/webform_analysis/src/Plugin/Block/WebformAnalysisBlock.php
------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
------------------------------------------------------------------------------------------------------------------------------
 61 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
 62 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead
------------------------------------------------------------------------------------------------------------------------------

Time: 410ms; Memory: 10MB

Looking deeper into this warning, I realized that in constructor had this code:

$this->entityTypeManager = $entityTypeManager ? $entityTypeManager : \Drupal::entityTypeManager();
$this->formBuilder = $formBuilder ? $formBuilder : \Drupal::formBuilder();

So it's already being done the dependency injection, but for some reason has this conditional calling Drupal. I think it's better to leave it at that, so I'm moving it to RTBC!

batigolix made their first commit to this issue’s fork.

  • batigolix committed cb9b980d on 8.x-1.x authored by anabpv
    Issue #2925863 by lbaran: Review and follow Coding Standards
    
batigolix’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.