Skip to content

Instantly share code, notes, and snippets.

@lukecarbis
Created November 7, 2017 05:48
Show Gist options
  • Save lukecarbis/545c4f28ee711967868970ac07ac768a to your computer and use it in GitHub Desktop.
Save lukecarbis/545c4f28ee711967868970ac07ac768a to your computer and use it in GitHub Desktop.
<?php
$weightings_file = 'https://raw.githubusercontent.com/xwp/wp-tide/develop/services/audit-server/audit-weightings/phpcs/tide/weightings.json';
$markdown_file = 'weightings.md';
if ( isset( $argv[1] ) ) {
$weightings_file = $argv[1];
}
if ( isset( $argv[2] ) ) {
$markdown_file = $argv[2];
}
/**
* Load the current weightings from the weightings json file
*
* @param string $weightings_file
* @return array
*/
function get_weightings( $weightings_file ) {
try {
$weightings = json_decode( file_get_contents( $weightings_file ), true );
} catch ( \Exception $e ) {
$weightings = array();
}
return $weightings;
}
/**
* Get sniff parent.
*
* @param string $sniff Sniff.
*
* @return string
*/
function get_sniff_parent( $sniff ) {
$sniff_parts = explode( '.', $sniff );
array_pop( $sniff_parts );
$sniff_parent = implode( '.', $sniff_parts );
return $sniff_parent;
}
/**
* Generate markdown based on weightings
*
* @param $weightings
*
* @return string
*/
function generate_weightings_markdown( $weightings ) {
$markdown = "# Tide Weighting Table\n\n";
$markdown .= "This table was automatically generated based on Tide's [weightings.json](https://github.com/xwp/wp-tide/blob/53083da4bf8362628c08b870c46d91b24af91077/services/audit-server/audit-weightings/phpcs/tide/weightings.json) file.\n\n";
$markdown .= "Category | Description | Weight\n";
$markdown .= "-------- | ----------- | ------\n";
foreach ( $weightings as $category => $category_config ) {
$markdown .= sprintf(
"%s|%s|%s\n",
$category,
$category_config['description'],
$category_config['weight']
);
}
$markdown .= "\n";
$markdown .= "Category | Sniff | Initial Loss | Subsequent Loss | Max Loss\n";
$markdown .= "-------- | ----- | ------------ | --------------- | --------\n";
foreach ( $weightings as $category => $category_config ) {
foreach ( $category_config['sniffs'] as $sniff => $sniff_config ) {
if ( ! isset( $sniff_config['weighting'] ) ) {
$sniff_parent = get_sniff_parent( $sniff );
$sniff_config = $category_config['sniffs'][ $sniff_parent ];
}
if ( ! isset( $sniff_config['weighting'] ) ) {
continue;
}
if ( ! isset( $sniff_config['initial'] ) ) {
$sniff_config['initial'] = $sniff_config['weighting'];
}
if ( ! isset( $sniff_config['max'] ) ) {
$sniff_config['max'] = '—';
}
$markdown .= sprintf(
"%s|`%s`|%s|%s|%s\n",
$category,
$sniff,
$sniff_config['initial'],
$sniff_config['weighting'],
$sniff_config['max']
);
}
}
return $markdown;
}
/**
* Write contents to file
*
* @param $filename
* @param $data
*/
function write_to_file( $filename, $data ) {
file_put_contents( $filename, $data );
}
$weightings = get_weightings( 'weightings.json' );
$markdown = generate_weightings_markdown( $weightings );
write_to_file( $markdown_file, $markdown );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment